3 Replies Latest reply: Dec 30, 2012 6:55 AM by Raja Pandian C RSS

    Issue : Read a text file and print the same

    Raja Pandian C
      Hi, My requirement is to read a text file and print it the same way.

      import java.io.BufferedReader;
      import java.io.FileReader;

      public class CatFile {

      public static void main(String[] args) throws Exception
      {
           
           FileReader file = new FileReader("D:/Test/Allfiles.txt");
           BufferedReader reader = new BufferedReader(file);
           
           String text = "";
           String line = reader.readLine();
           while (line != null)
           {
                text += line;
                line = reader.readLine();
           }
           
           System.out.println(text);

      }
      }


      The text file i used contains
      A
      B
      C

      but my output is ABC.

      What change should be made to print it the same way in the txt file ?