Java 使用 PrintStream 附加到文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9805021/
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-16 07:50:06  来源:igfitidea点击:

Append to text file using PrintStream

javaappendprintstream

提问by Twistar

I cant append text to a text file, it only overwrites the previous text. My code:

我无法将文本附加到文本文件,它只会覆盖之前的文本。我的代码:

//using JFileChooser to select where to save file
PrintStream outputStream = MyFrame.ShowSaveDialog();
    if(outputStream!=null){
        outputStream.append(input);
        outputStream.close();
    } 

Edited: The ShowSaveDialog returns a PrintStream. Here is the code for that method:

编辑: ShowSaveDialog 返回 PrintStream。这是该方法的代码:

public static PrintStream ShowSaveDialog(){
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Tekst filer", "txt");
    chooser.setFileFilter(filter);

    int returnVal = chooser.showSaveDialog(null);
    try{
        if(returnVal == JFileChooser.APPROVE_OPTION){

            return new PrintStream(chooser.getSelectedFile());              
        }
        else{
            return null;
        } 
    }
    catch(FileNotFoundException e){
        JOptionPane.showMessageDialog(null, "Ugyldig Fil!",
                   "error", JOptionPane.ERROR_MESSAGE);
    }
    return null;

}

采纳答案by Hovercraft Full Of Eels

What does MyFrame.ShowSaveDialog();return? The key is to create a FileOutputStream with the appropriate constructor (the second parameter should be the boolean true) which will make it an appending FileOutputStream, and then construct your PrintStream using this FileOutputStream object.

什么MyFrame.ShowSaveDialog();回报?关键是使用适当的构造函数(第二个参数应该是 boolean true)创建一个 FileOutputStream,这将使它成为一个附加的 FileOutputStream,然后使用这个 FileOutputStream 对象构造您的 PrintStream。

For instance, if showSaveDialog() (note that method and variable names should begin with lower case letters) returns the name of a file or a File object, you could do something like so:

例如,如果 showSaveDialog()(注意方法和变量名称应以小写字母开头)返回文件或 File 对象的名称,您可以执行以下操作:

try {
  File file = myFrame.showSaveDialog(); // if this method returns a File!!!!!
  FileOutputStream fos = new FileOutputStream(file, true);
  PrintStream printStream = new PrintStream(fos);
  //.... etc
} catch(....) {
  // ....
}

Edit:
To apply this to your posted code above, do something like so:

编辑:
要将其应用于上面发布的代码,请执行以下操作:

   public static PrintStream showSaveDialog() {
      JFileChooser chooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Tekst filer", "txt");
      chooser.setFileFilter(filter);

      int returnVal = chooser.showSaveDialog(null);
      try {
         if (returnVal == JFileChooser.APPROVE_OPTION) {

            //  ******* note changes below *****
            File file = chooser.getSelectedFile();

            FileOutputStream fos = new FileOutputStream(file, true);
            return new PrintStream(fos);
         } else {
            return null;
         }
      } catch (FileNotFoundException e) {
         JOptionPane.showMessageDialog(null, "Ugyldig Fil!", "error",
               JOptionPane.ERROR_MESSAGE);
      }
      return null;

   }

The crux would be these lines here:

关键是这里的这些行:

            File file = chooser.getSelectedFile();
            FileOutputStream fos = new FileOutputStream(file, true);
            return new PrintStream(fos);

The true in the FileOutputStream constructor creates a FileOutputStream that appends to the existing file. Please check out the FileOutputStream API for the details on this.

FileOutputStream 构造函数中的 true 创建一个附加到现有文件的 FileOutputStream。请查看 FileOutputStream API 以了解有关此内容的详细信息。