0

I have a text file where text file Looks like output.txt

  | Component  | Tests Run   | Tests Failed                                   |
  |:-----------|:-----------:|:-----------------------------------------------|
  | Server     | 948         | :white_check_mark: 0                           |
  | Web Client | 123         | :warning: 2 [(see details)](http://linktologs) |
  | iOS Client | 78          | :warning: 3 [(see details)](http://linktologs) |

Here my work purpose I need to push all the code somewhere so that it will show like a table I want to read all the text from the text file and print the text together.

Currently its print line by line

try {
                FileReader reader = new FileReader("C:\\Users\\Zsbappa\\Pictures\\test\\output.txt");
                int line;

                while ((line = reader.read()) != -1) {
                    System.out.print((char) line);
                }
                reader.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

Any suggestion will really appreciated

Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52

3 Answers3

2

If you are on Java 8 or beyond, read them as a stream:

Files.lines(Paths.get("C:\\Users\\Zsbappa\\Pictures\\test\\output.txt")).forEach(System.out::println);
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
1

Try using:

FileInputStream inputStream = new FileInputStream("output.txt");
try {
   System.out.println(IOUtils.toString(inputStream));
} finally {
    inputStream.close();
}

The IOUtils class is part of the Apache Commons IO. It can be downloaded here.

TVASO
  • 481
  • 3
  • 15
  • Its throwing error Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3332) – Zakaria Shahed Aug 06 '18 at 18:23
1

According to your question, you want to read the file all at once, but only print it line by line. I'm not sure why you would want to do this, but this should work:

    try {
        File file = new File("C:\\Users\\Zsbappa\\Pictures\\test\\output.txt");
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        fis.read(data);
        fis.close();

        String str = new String(data, "UTF-8");

        String lines[] = str.split("\\r?\\n");
        for (String line : lines) {
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

This will be quite inefficient for large text files and will eat up your RAM, but this is the best solution I could think of for your specific problem.

Also remember to import File and FileInputStream since they are new standard library dependencies you are using.

agillgilla
  • 859
  • 1
  • 7
  • 22
  • Actually, I am integration a massager where if get all the output file and send it its will formatting like a table. This is my purpose.It will be not a big text file..Lets check your program – Zakaria Shahed Aug 06 '18 at 18:40
  • got an exception Exception in thread "main" java.lang.NegativeArraySizeException in this line byte[] data = new byte[(int) file.length()]; – Zakaria Shahed Aug 06 '18 at 18:42
  • Does the file have anything in it? Is it corrupted? I don't see how getting the length of the file would return a negative number unless there is something wrong with your file. – agillgilla Aug 06 '18 at 18:45
  • Same text like output – Zakaria Shahed Aug 06 '18 at 18:45
  • I just edited my solution to have a proper `try` `catch` so that the error handling is done. I copied your `output.txt` file to my computer and ran the program on it. It printed fine. Are you sure yours isn't corrupted (or that the path isn't messed up)? – agillgilla Aug 06 '18 at 18:50
  • sorry, I was missing the path. It's working perfectly for me now.Thanks, lots – Zakaria Shahed Aug 06 '18 at 18:52