Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying existing file content in Java

Tags:

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 

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 
like image 718
user1106130 Avatar asked Dec 19 '11 15:12

user1106130


People also ask

How do I edit an existing file in Java?

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.

How do you replace contents of a file in Java?

Invoke the replaceAll() method on the obtained string passing the line to be replaced (old line) and replacement line (new line) as parameters. Instantiate the FileWriter class. Add the results of the replaceAll() method the FileWriter object using the append() method.

How do I add content to an existing file in Java?

You can append text into an existing file in Java by opening a file using FileWriter class in append mode. You can do this by using a special constructor provided by FileWriter class, which accepts a file and a boolean, which if passed as true then open the file in append mode.


1 Answers

As proposed in the accepted answer to a similar question:

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+"\n");          }       } 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);     } } 
like image 73
melihcelik Avatar answered Oct 16 '22 19:10

melihcelik