在文本文件中写入多行(java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21813434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:49:01  来源:igfitidea点击:

Write multiple lines in a text file ( java)

java

提问by Laura Ruby

This following code is to run a command in cmdand 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?

以下代码用于运行命令cmd并生成带有命令行输出的文本文件。下面的代码在 Eclipse 的输出窗口中显示了正确的信息,但在文本文件中只打印了最后一行。谁能帮我这个?

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();

    }

   }
}

回答by David Koelle

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.

问题是您正在循环中创建一个新的 BufferedWriter。其次,您还关闭了循环内的编写器。最后,您的代码块有点令人困惑——如果您重新格式化代码(在 Eclipse 中使用 Ctrl-F 自动完成),代码中的块将更加清晰。

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

您的代码应如下所示,位于trytry/catch 块的部分内:

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

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

回答by Vikas Sachdeva

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

正如其他答案中所述,问题是您BufferedWriter在 while 循环中创建实例,这就是为什么只有最后一行被打印到文件中。

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

使用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();

        }
    }
}