public static void main(String[] args){
File writename = new File("D:\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件
try {
if(!writename.exists()){
writename.createNewFile(); // 如果不存在·创建新文件
}
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write("我会写入文件啦\r\n"); // \r\n即为换行
out.write("记录类型"+"|");
out.write("报盘类型"+"|");
out.write("结算期"+"|");
out.write("总笔数"+"|");
out.write("总金额"+"|");
out.write("报盘日期"+"|");
out.write("业务摘要"+"|");
out.write("备注\r\n");
out.write("业务摘要"+"|");
out.close(); // 最后记得关闭
} catch (IOException e) {
e.printStackTrace();
}
}
读
public static void readFile() {
String pathname = "input.txt"; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件
//防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw;
//不关闭文件会导致资源的泄露,读写文件都同理
//Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271
try (FileReader reader = new FileReader(pathname);
BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言
) {
String line;
//网友推荐更加简洁的写法
while ((line = br.readLine()) != null) {
// 一次读入一行数据
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}