使用 PrintStream 附加到文本文件 [英] Append to text file using PrintStream
问题描述
我无法将文本附加到文本文件中,它只会覆盖之前的文本.我的代码:
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();
}
ShowSaveDialog 返回一个 PrintStream.这是该方法的代码:
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;
}
推荐答案
MyFrame.ShowSaveDialog();
返回什么?关键是使用适当的构造函数创建一个 FileOutputStream(第二个参数应该是布尔值 true
),这将使它成为一个附加的 FileOutputStream,然后使用这个 FileOutputStream 对象构造你的 PrintStream.
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.
例如,如果 showSaveDialog()(注意方法和变量名称应以小写字母开头)返回文件或 File 对象的名称,您可以执行以下操作:
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(....) {
// ....
}
要将其应用于上面发布的代码,请执行以下操作:
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;
}
关键是这里的这些行:
File file = chooser.getSelectedFile();
FileOutputStream fos = new FileOutputStream(file, true);
return new PrintStream(fos);
FileOutputStream 构造函数中的 true 会创建一个附加到现有文件的 FileOutputStream.有关详细信息,请查看 FileOutputStream API.
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.
这篇关于使用 PrintStream 附加到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!