且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

追加到使用PrintStream的文本文件

更新时间:2022-11-18 22:17:14

这是什么 MyFrame.ShowSaveDialog(); 返回?关键是要建立一个FileOutputStream与适当的构造函数(第二个参数应该是布尔真正),这将使它成为一个附加的FileOutputStream,然后使用这个FileOutputStream中构建你的PrintStream对象。

例如,如果showSaveDialog()(注意方法和变量名应以小写字母开头)返回一个文件或文件对象的名称,你可以不喜欢这样:

  {尝试
  文件fil​​e = myFrame.showSaveDialog(); //如果这个方法返回一个文件!!!!!
  FOS的FileOutputStream =新的FileOutputStream(文件,真正的);
  为PrintStream PrintStream的=新的PrintStream(FOS);
  // ....等
}赶上(....){
  // ....
}

编辑:结果
以上将此你贴code,做一些像这样:

 公共静态的PrintStream showSaveDialog(){
      JFileChooser的选择器=新的JFileChooser();
      的FileNameExtensionFilter过滤器=新的FileNameExtensionFilter(
            Tekst文件管理器,TXT);
      chooser.setFileFilter(过滤器);      INT returnVal = chooser.showSaveDialog(NULL);
      尝试{
         如果(returnVal == JFileChooser.APPROVE_OPTION){            // *******以下注意事项的变化*****
            文件fil​​e = chooser.getSelectedFile();            FOS的FileOutputStream =新的FileOutputStream(文件,真正的);
            返回新的PrintStream(FOS);
         }其他{
            返回null;
         }
      }赶上(FileNotFoundException异常五){
         JOptionPane.showMessageDialog(NULL,Ugyldig费尔!,错误,
               JOptionPane.ERROR_MESSAGE);
      }
      返回null;   }

的关键是这些线路在这里:

 文件文件= chooser.getSelectedFile();
            FOS的FileOutputStream =新的FileOutputStream(文件,真正的);
            返回新的PrintStream(FOS);

在FileOutputStream中构造真正创建一个附加到现有文件一个FileOutputStream。对于这个细节,请检查出的FileOutputStream API。

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:

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;

}

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.

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:

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.