import java.io.*; public class FileExample { public static void main (String [] args) { BufferedReader br; FileReader fr; FileWriter fw; PrintWriter pw; String s = ""; String wholeString = ""; int i = 1; try { fr = new FileReader("data.txt"); br = new BufferedReader(fr); // Read text from file and append on line count and // newlines while (s != null) { s = br.readLine(); if (s != null) { System.out.println(s); wholeString = wholeString + i + " " + s + "\n"; } i = i + 1; } br.close(); } catch (FileNotFoundException e) { System.out.println("data.txt not in current folder"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Write the appended text from above to file (includes line // numbering and extra newlines). try { fw = new FileWriter("data_appended.txt"); pw = new PrintWriter(fw); pw.write(wholeString); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }