File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes File IO can't handle Strings? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File IO can Watch "File IO can New topic
Author

File IO can't handle Strings?

Kenneth Kim
Greenhorn

Joined: Aug 11, 2005
Posts: 10
I created a method called readFile as shown below:



And in my GUI, the pertinent code is like this:



My question is:

a) Is my method for reading a file and converting it into a string correct?
b) Why do I get the error message readFile(java.io.File) cannot be applied to java.lang.String?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

a) InputStreams are for reading raw data, while Readers are for reading text. Although what you've written works OK for ASCII files, it won't work for other character sets, limiting the portability of your code. You can instead write something like



b) You've declared your method to accept a java.io.File object as an argument, but you're passing a String. You need to say

readFile(new File(fileName));


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Kenneth Kim:
... Why do I get the error message readFile(java.io.File) cannot be applied to java.lang.String?

Your readFile method is expecting a reference to an instance of File -- not String. But you should be able to use that Sting to make a new instance of File, and then pass the File reference to your method.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
While we're at it, the read(byte[]) method has a return value. You should not assume that the byte[] array has been filled after one read - there are several reasons why a read may be incomplete. If you want to get a complete read from an InputStream, you need to check the return value and keep reading until you're done.

Or more simply, you can use a RandomAccessFile which has a readFully() method - somewhat simpler to use.

Also, I strongly recommend putting putting the close() statement in a close() block. If an error occurs, you generally want to make sure the file gets closed anyway. This is particularly important if you try to do something else with the file afterwards, such as moving it or deleting it.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:

Also, I strongly recommend putting putting the close() statement in a close() block.


Jim probably meant a *finally block* here...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Something like that, yes... Thanks, Ilja.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
FYI, java.util.Scanner can be used to read the file in one step:
http://java.sun.com/developer/JDCTechTips/2004/tt1201.html#1


There is no emoticon for what I am feeling!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Slightly more than one step, Jeff, but it's simpler, that's true. Note that Scanner became available in JDK 5.

I also note that I overlooked the fact that EFH's code already incorporated checking the return value and using a finally block. Oh, well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File IO can't handle Strings?