大家好我试图将数据保存在记事本中,但我不知道如何保存多行。使用这段代码我只能保存一次数据。如何将数据保存到java的记事本
package Vista;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class notepad_Data {
public void escribir(String nombreArchivo) {
File f;
f = new File("save_data");
try {
FileWriter w = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(w);
PrintWriter wr = new PrintWriter(bw);
wr.append(nombreArchivo+" ");
bw.close();
} catch (IOException e) {
};
}
public static void main(String[] args){
notepad_Data obj = new notepad_Data();
obj.escribir("writing in the notepad");
}
}
我试图与此代码在escribir方法,但不工作
for(int i=0; i<1000; ++i){
try {
FileWriter w = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(w);
PrintWriter wr = new PrintWriter(bw);
wr.append(nombreArchivo+" ");
bw.close();
} catch (IOException e) {
};
}
的[如何将文本追加到Java中的现有文件]可能的复制(https://stackoverflow.com/questions/1625234/how-to-append-text-to-an -existing-file-in-java) –
永远不要写一个空的catch子句'catch(Exception ex){}';如果发生异常,你永远不会知道。至少把'ex.printStackTrace();'放在空括号内,这样你会看到是否发生异常。 –