package file_io; import java.io.*; public class TestFileIO { /** Creates and starts a new TestFileIO application. */ public TestFileIO () { try { PrintWriter writer = new PrintWriter ("J:/Documents/ics/111/test.txt"); writer.println ("Hi from HI"); writer.println ("Hi from ics 111"); writer.close (); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static String readFileByLine () { try { File file = new File ("J:/Documents/ics/111/test.txt"); InputStream input = new FileInputStream(file); try { LineNumberReader reader = new LineNumberReader (new InputStreamReader(input)); String text = ""; String line = reader.readLine(); for (; line != null; line = reader.readLine()) { text += line + "\n"; } // now text contains the contents of the file return text; } catch (IOException error) { //handle reading error } finally { try {input.close (); } catch (IOException error) {} //handle closing error } } catch (FileNotFoundException error) { //handle non-existing file error } return null; } public static void main (String [] arguments) { new TestFileIO (); String text = readFileByLine (); System.out.println (text); } }