我正在构建一个命令 shell,在这个命令 shell 中我想保存我写入文件的每个命令。
我读的命令是这样的:
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
commandLine = console.readLine();
我像这样写入文件:
public static void fileWriter(String e){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("errorLog.txt"));
bw.write(e);
bw.flush();
bw.close();
}
catch(IOException ex){
System.out.println(ex);
}
}
问题是,每个命令都会覆盖最后一个命令,因此我无法保存在命令 shell session 期间写入的每个命令。我需要的是我编写的所有命令的列表。
我已经用谷歌搜索了大约两个小时,但找不到任何东西。请帮忙。
最佳答案
使用
BufferedWriter bw = new BufferedWriter(new FileWriter("errorLog.txt",true));
true
告诉 FileWriter 追加数据而不是覆盖。请参阅文档。
public FileWriter(String fileName,boolean append) throws IOException
Constructs a FileWriter object given a file name with a boolean
indicating whether or not to append the data written.
关于java - 将输入保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286723/