有勇气的牛排博客
☰

java 操作txt文件

有勇气的牛排 993 Java 2023-05-17 23:39:39

知止自律打卡-微信小程序APP
一款微信自律打卡小程序,支持自定义打卡项目、习惯记录、项目日记、专注计时与数据统计。自律打卡、学习打卡、读书打卡、健身打卡、运动打卡、减肥打卡、戒色打卡、养生打卡、戒烟、戒酒、戒熬夜等多种场景。万能打卡模板自由创建。可自定义每日打卡目标,记录习惯连续天数,可视化追踪成长进度。
了解产品详情 →
微信搜索「知止自律打卡」
知止自律打卡微信小程序

按行读取

public static void readFile(){ String pathname = "E:/project/java/idea项目/java学习/test.txt"; String line; try{ FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader); while ((line=br.readLine())!=null){ System.out.println(line); } } catch (IOException e){ e.printStackTrace(); } }

写入文件

public static void writeFile(){ try{ String pathname = "E:/project/java/idea项目/java学习/test.txt"; File writeName = new File(pathname); FileWriter writer = new FileWriter(writeName); BufferedWriter out = new BufferedWriter(writer); out.write("666"); out.write("1\r2\n3"); out.flush(); // 把缓存区内容压入文件 } catch (IOException e){ e.printStackTrace(); } }

文件内容:
image.png

评论区

×
×