import java.io.*;
import java.util.*;
public class RW {
////////////属性/////////////
String tile;
String userName;
String information;
////////////主函数/////////////
public static void main(String[] arys) {
Scanner sn = new Scanner(System.in);
////////////集合/////////////
RW rw = new RW();
System.out.print( "请输入标题: ");
rw.tile = sn.next();
System.out.print( "请输入姓名: ");
rw.userName = sn.next();
System.out.print( "请输入内容: ");
rw.information = sn.next();
ArrayList arr = new ArrayList();
arr.add( "标题: "+rw.tile);
arr.add( "姓名: "+rw.userName);
arr.add( "内容: "+rw.information);
System.out.println( " ");
////////////写入/////////////
try {
FileWriter fw = new FileWriter( "C:\\Message.txt ");
BufferedWriter bw = new BufferedWriter(fw);
String wt = " ";
for (int i = 0; i < arr.size(); i++) {
wt=(String) arr.get(i);
bw.write(wt);
bw.newLine();//这个是换行相当于 "\n "
}
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
////////////读取/////////////
System.out.println( "显示帖子内容 ");
try {
FileReader fr = new FileReader( "C:\\Message.txt ");
BufferedReader br = new BufferedReader(fr);
String inf= " ";
while ((inf = br.readLine()) != null) {
System.out.println(inf);
}
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我做了一个输入输出,每次运行“Message.txt”内的内容都会刷新,请问怎么样才能不刷新内容而是继续添加,保留原来的?
谢谢
------解决方案--------------------
////////////写入/////////////
try {
FileWriter fw = new FileWriter( "C:\\Message.txt ",true);
BufferedWriter bw = new BufferedWriter(fw);
String wt = " ";
for (int i = 0; i < arr.size(); i++) {
wt=(String) arr.get(i);
bw.write(wt);
bw.newLine();//这个是换行相当于 "\n "
}
------解决方案--------------------
上面是正经的办法.
最笨的办法,就是先读出来,然后+一下.再写回去