java向文件中追加数据内容。
使用filewrite()方法向文件中追加数据
完整代码
import java.io.*;public class Main {public static void main(String[] args) throws Exception {try {BufferedWriter out = new BufferedWriter(new FileWriter("filename"));out.write("firstString\n");out.close();out = new BufferedWriter(new FileWriter("filename",true));out.write("secondString");out.close();BufferedReader in = new BufferedReader(new FileReader("filename"));String str;while ((str = in.readLine()) != null) {System.out.println(str);}in.close();}catch (IOException e) {System.out.println("exception occoured"+ e);}}
}
结果输出
firstString
secondString