3

This following code is to run a command in cmd and to produce a text file with the output from the command line. The code below shows the correct information in the output window in Eclipse, but only one last line is printed in the text file. Can anyone help me with this?

import java.io.*;

public class TextFile {
public static void main(String[] args) throws IOException {
    try {
        Process p = Runtime.getRuntime().exec("git log");

        BufferedReader in = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
        BufferedWriter writer = null;

        String line = null;
        while ((line = in.readLine()) != null) {

            writer = new BufferedWriter(new FileWriter("textfile.txt"));
            writer.write(line);
            System.out.println(line);

            writer.close();
        }

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

    }

   }
}
2
  • 2
    By creating a FileWriter in each iteration, the file is overwritten. Declare the Writers outside the loop and close it after the loop. (Btw; your code is very hard to read, please obey to some consistent indention the next time, this makes it easier to spot such errors.) Commented Feb 16, 2014 at 15:49
  • Please note that nowadays one should use the new IO library of java, called NIO. The core classes are Paths, Path and Files. You can, for example find files with Paths.get("filename") or write to a file with Files.write(pathToFile, stuffToWrite, options). Commented Oct 8, 2017 at 14:35

2 Answers 2

3

As stated in other answer, problem is that you are creating BufferedWriter instance inside while loop and that is why only last line is getting printed in file.

One of the better way to write this program using try-with-resources -

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class TextFile {

    public static void main(String[] args) throws IOException {
        try {
            Process p = Runtime.getRuntime().exec("git log");

            try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    BufferedWriter writer = new BufferedWriter(new FileWriter("textfile.txt"))) {

                String line = null;
                while ((line = in.readLine()) != null) {
                    writer.write(line);
                    writer.newLine();
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you are creating a new BufferedWriter within the loop. Secondarily, you are also closing the writer inside the loop. Finally, your code blocks are a little confusing - if you reformat your code (do it automatically with Ctrl-F in Eclipse), the blocks in your code will be more clear.

Your code should look like this, inside the try portion of the try/catch block:

    BufferedWriter writer = (new FileWriter ("textfile.txt"));

    String line =null;
    while ((line= in.readLine()) !=null) {
        writer.write(line);
        System.out.println(line);
    }
    writer.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.