在 Java 中修改现有文件内容 [英] Modifying existing file content in Java

查看:22
本文介绍了在 Java 中修改现有文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换第二行文件内容,有人可以根据以下文件格式和监听器方法帮忙吗.

I want to replace the second line file content, can somebody help please based on the below file format and listener method.

1324254875443
1313131
Paid
0.0

第二行很长,想替换为currentTimeMillis().

2nd line is long and want to replace to currentTimeMillis().

/************** Pay Button Listener **************/
public class payListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        ArrayList<String> lines = new ArrayList<String>();
        String line = null;

        try {
            FileReader fr = new FileReader("Ticket/" + ticketIDNumber + ".dat");
            BufferedReader br = new BufferedReader(fr);
            FileWriter fw = new FileWriter("Ticket/" + ticketIDNumber + ".dat");
            BufferedWriter bw = new BufferedWriter(fw);
            while ((line = br.readLine()) != null) {
                if (line.contains("1313131"))
                    line.replace(System.currentTimeMillis();
                lines.add(line);
                bw.write(line);
            }  //end if                 

        }     //end try
        catch (Exception e) {

        }  //end catch   
    }    //end while
}//end method

推荐答案

接受的答案中所提议的一个类似的问题:

同时以写方式打开一个临时文件,对每一行读取,必要时修改,然后写入临时文件.最后删除原文件,重命名临时文件.

open a temporary file in writing mode at the same time, and for each line, read it, modify if necessary, then write into the temporary file. At the end, delete the original and rename the temporary file.

基于您的实现,类似于:

Based on your implementation, something similar to:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReplaceFileContents {
   public static void main(String[] args) {
     new ReplaceFileContents().replace();
   }

   public void replace() {
      String oldFileName = "try.dat";
      String tmpFileName = "tmp_try.dat";

      BufferedReader br = null;
      BufferedWriter bw = null;
      try {
         br = new BufferedReader(new FileReader(oldFileName));
         bw = new BufferedWriter(new FileWriter(tmpFileName));
         String line;
         while ((line = br.readLine()) != null) {
            if (line.contains("1313131"))
               line = line.replace("1313131", ""+System.currentTimeMillis());
            bw.write(line+"
");
         }
      } catch (Exception e) {
         return;
      } finally {
         try {
            if(br != null)
               br.close();
         } catch (IOException e) {
            //
         }
         try {
            if(bw != null)
               bw.close();
         } catch (IOException e) {
            //
         }
      }
      // Once everything is complete, delete old file..
      File oldFile = new File(oldFileName);
      oldFile.delete();

      // And rename tmp file's name to old file name
      File newFile = new File(tmpFileName);
      newFile.renameTo(oldFile);

   }
}

这篇关于在 Java 中修改现有文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆