寂寞不孤单
2016-01-06 14:46
采纳率: 16.7%
浏览 2.0k
已采纳

利用java io逐行操作文件并替换内容

A文件:
as1
dfg1
hjkl1
逐行读取A文件的内容,将1替换成 一 后将替换后的内容写到B文件中

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 男哥 2016-01-07 06:35
    已采纳

    BufferedReader readBuffer = new BufferedReader(new FileReader( new File("D:/aaaa.txt")));
    BufferedWriter write = new BufferedWriter(new FileWriter(new File("D:/bbbb.txt")));
    String line = readBuffer.readLine();
    while(line!=null){
    write.write(line.replaceAll("1", "替换后")+"\r\n");
    line = readBuffer.readLine();
    }
    write.flush();

        不谢;
    
    点赞 打赏 评论
  • blownewbee 2016-01-06 15:24
     File file = new File(输入文件);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    BufferedWriter bw = new BufferedWriter(new FileWriter(输出文件));
    String tempString = null;
    while ((tempString = reader.readLine()) != null) {
        wr.writeLine(tempString.replace("1", "替换后"));
    }
    
    
    点赞 打赏 评论
  • 鼬佐 2016-01-07 01:13

    1楼应该写的是bw,

     while ((tempString = reader.readLine()) != null) {
        bw.write(tempString.replace("1", "替换后"));
            bw.newLine();
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题