Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File IO can't handle Strings?

 
Kenneth Kim
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like that, yes... Thanks, Ilja.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI, java.util.Scanner can be used to read the file in one step:
http://java.sun.com/developer/JDCTechTips/2004/tt1201.html#1
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic