Java Training: Input/Output Solutions
Streams Lab. Solution for reading ASCII file
/**
* This class purpose is to introduce two static methods
* capable to read and write an ASCII file
* @@author Jeff Zhuk
**/
public class StreamsLab
{
/**
* for an application ONLY
* read a file into a string
*
* @param iFilename
* @return string of text, or null if failure
**/
public static String readFile(String iFilename)
{
try
{
BufferedReader in =
new BufferedReader(new FileReader(iFilename));
String text = ""; // start reading with empty string
String line = null; // init line to read
while((line = in.readLine()) != null) { // end of file
text += line + "\r\n";
// windows line ending
// for UNIX it would be just "\n"
}
return text;
}
catch (IOException e)
{
System.err.println("readFile failure: " + e);
return null;
}
}