-1

I would like to be able to display the content of a .txt file in my output terminal (Using the System.out.print() command).

I have already tried the fileInputStream /outputStream but I either didn't used it correctly or it didn't work out as I thought it would.

The goal would be to display the content in my file with the same line feed, etc...

  • 3
    Please add your current code to your question . – Arnaud Jul 08 '19 at 14:09
  • 1
    Checkout the great Mkyong article [Java – Read a text file line by line](https://www.mkyong.com/java/java-read-a-text-file-line-by-line/). – Tim Biegeleisen Jul 08 '19 at 14:10
  • 2
    https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html – Alexey Usharovski Jul 08 '19 at 14:10
  • InputStream is great for reading bytes, but if you want to read text, a Reader is a better choice. Either wrap your stream in a `InputStreamReader` or use a `FileReader` – killjoy Jul 08 '19 at 14:10
  • Thank you for all of your answers, I'm sorry for not adding my code to my question, I am not entirely familiar with this platform yet :). – Mr.Viatorem Jul 08 '19 at 14:26
  • Add it as best you can, and the community can help with formatting – Gus Jul 08 '19 at 14:34

1 Answers1

0

Just use the scanner (or reader) to read each line of the text file and then print it out as so.

    String path = "testFilePath.txt";
    try (Scanner scanner = new Scanner(new File(path))) {
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
killjoy
  • 3,665
  • 1
  • 19
  • 16
omoshiroiii
  • 643
  • 5
  • 11