比如test.txt,内容是:
111
222
333
我现在需要把333换成444,333默认是在最后一行,请问各位大佬们怎么才能修改,最好有相关代码,谢谢各位啦

如何用java修改txt的最后一行数据
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- 我是一只小小小小小鸟 2017-11-24 03:29关注
public static void main(String[] args) throws IOException{ FileReader fr = new FileReader(new File("e:/Test/java/test1.txt")); BufferedReader br = new BufferedReader(fr); String s; StringBuilder sb = new StringBuilder(); int i = 0; //读文件内容 while((s=br.readLine())!=null){ sb.append(s).append("\n"); } //对内容进行截取去掉最后一个\n,然后截取去掉最后一行,最后加入你想要的内容 System.out.println(sb.substring(0, sb.toString() .substring(0, sb.length()-1).lastIndexOf("\n"))+"\n4444"); fr.close(); br.close(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报