Read the txt and return it all in a String in Java [duplicate]

0

I have this code to read txt and I would like to return a string with the txt all copied, but this algorithm it only read the lines and shows how to return all what content did it read in a string variable?

  public static void main (String[]args){
      String linha = null;

      try{
         BufferedReader br = new BufferedReader(new FileReader("c:/algoritmo.txt"));
         while(br.ready()){
             linha= br.readLine();
            System.out.println(linha);

         }
         br.close();
      }catch(IOException ioe){
         ioe.printStackTrace();

      }
      System.out.println());}
}
    
asked by anonymous 09.09.2017 / 03:59

1 answer

2

It's a lot simpler than that:

System.out.println(new String(Files.readAllBytes(Paths.get("c:/algoritmo.txt"))));

Documentation .

    
09.09.2017 / 04:05