Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:57:03+00:00 2026-05-27T20:57:03+00:00

I want to replace the second line file content, can somebody help please based

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:57:04+00:00Added an answer on May 27, 2026 at 8:57 pm

    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);
    
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a text file with a line default_color acolor and I want to replace
I want to replace the first context of web/style/clients.html with the java String.replaceFirst method
I have a huge text file, size > 4GB and I want to replace
I want to take in a certain format of one text file. After the
The code below is not a functioning method it's just written to help you
I have a text file looks like: first line second line third line forth
I want to replace the 'client' field text box in fogbugz when you edit
I want to replace RBSplitView with NSSplitView in my existing project. The application is
I want to replace the last String which is a , with ) .
I want to replace and insert escape character sequences with their escape character values,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.