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();
}
}
}
NIO. The core classes arePaths,PathandFiles. You can, for example find files withPaths.get("filename")or write to a file withFiles.write(pathToFile, stuffToWrite, options).