Java 功能“;writeFile(字符串文本)";不';第二次使用时不能工作
在您称之为副本之前,请确认以下事实:Java 功能“;writeFile(字符串文本)";不';第二次使用时不能工作,java,save,filewriter,printwriter,Java,Save,Filewriter,Printwriter,在您称之为副本之前,请确认以下事实: 没有布尔值 在我手动读取文本文件后,它们丢失了应该写入文件的第二个字符串 以下是重要的代码: package StorageBox; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util
package StorageBox;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class StorageBox02 {
String FileName;
PrintWriter pw;
Scanner sc;
public StorageBox02(){
}
public StorageBox02(String FileName) {
this.FileName = FileName;
}
public void writeFile(String Text) throws IOException{
FileWriter fw = new FileWriter(FileName);
pw = new PrintWriter (fw);
pw.write(Text + "\t");
pw.close();
}
}
第二个字符串是当我运行writeFile
方法两次时的预期结果
在我手动读取文本文件后,它们丢失了第二个
应该写入文件的字符串
似乎您正在尝试附加到以前编写的文件。在这种情况下,您需要通过将true
传递到PrintWriter
以追加模式打开文件
例如,
newprintwriter(fw,true)代码>第二个字符串是什么?这两种方法都只对文件执行一次写入(无附加),然后将其关闭。调用其中任何一个方法都将覆盖以前编写的任何内容。不是您要求的内容,而是。。。。有两个内容相同的方法似乎是多余的。只需使用这两种方法中的一种,并调用两次即可。我绝望了,所以我想尽一切办法;不多