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
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.
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.
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.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With