This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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
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.
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