Forums Register Login

File IO can't handle Strings?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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));
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
Something like that, yes... Thanks, Ilja.
+Pie Number of slices to send: Send
FYI, java.util.Scanner can be used to read the file in one step:
http://java.sun.com/developer/JDCTechTips/2004/tt1201.html#1
+Pie Number of slices to send: Send
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.
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 990 times.
Similar Threads
Merge and split file
directly saving an image to a file?
How can I create an object based on input
java.lang.OutOfMemoryError
Java: Convert a binary file to "text" and back again.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:10:44.