Java 实例 – 向文件中追加数据

[

Java 实例 - 向文件中追加数据

Java 实例
以下实例演示了使用 filewriter 方法向文件中追加数据:

Main.java 文件

import java.io.*;
 
public class Main {
public static void main(String[] args) throws Exception {
try {
BufferedWriter out = new BufferedWriter(new FileWriter("filename"));
            out.write("aString1\n");
            out.close();
            out = new BufferedWriter(new FileWriter("filename",true));
            out.write("aString2");
            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);
        }
}
}

以上代码运行输出结果为:


aString1

aString2

Java 实例

]
转载请保留页面地址:https://www.breakyizhan.com/java/23843.html

posted on 2020-07-12 00:54  MrAit  阅读(591)  评论(0编辑  收藏  举报

导航