The Artima Developer Community
Sponsored Link

Java Answers Forum
replace a sustring

6 replies on 1 page. Most recent reply: Jun 26, 2009 12:07 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
priya ana

Posts: 5
Nickname: apriyanka
Registered: Jun, 2009

replace a sustring Posted: Jun 17, 2009 12:23 AM
Reply to this message Reply
Advertisement
hey.
i am new to java
just want help to change a substring in file
my file contains string like
Error_Code :0001 occured at 11:21:53:750
so every time i read this file need to update the time of this error code occurance.

Please help me as already tired replace() method.


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: replace a sustring Posted: Jun 17, 2009 2:27 AM
Reply to this message Reply
Open the input file.
Create an output file.

Loop until the end of the input file is reached.
Read a line from the input file.
If the line contains the error code you're checking then update the date on that line.
Write the line to the output file
End loop

Close the output file.
Close the input file.

(Replace the old input file with the new output file, if required.)

priya ana

Posts: 5
Nickname: apriyanka
Registered: Jun, 2009

Re: replace a sustring Posted: Jun 18, 2009 1:51 AM
Reply to this message Reply
Hi thanks for the response..
but can u let me know more in detail...
sample code for the same will be very helpful

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: replace a sustring Posted: Jun 19, 2009 1:53 AM
Reply to this message Reply
Here's some sample code. I haven't tested it but just typed it in an got rid of any obvious syntax errors.
    static void processFile(String filename) throws IOException
    {
        // Open the input File.
        File inputFile  = new File(filename);
        BufferedReader input  = new BufferedReader(new FileReader(inputFile));
        // Create the output file.
        File outputFile = new File("temporary file");
        BufferedWriter output = new BufferedWriter(new FileWriter(outputFile));
        
        // Loop until the end of the input file is reached,
        // reading a line of data from the input file each time.
        String lineOfData;
        while ((lineOfData = input.readLine()) != null)
        {
            // If the line contains the error code you're checking 
            // then update the date on that line.
            if (lineOfData.contains("Error_Code :0001"))
            {
                // This is just an example.  You will need to format the date 
                // to suit your needs.
                lineOfData = "Error_Code :0001 occurred at " + (new Date()).toString();
            }
            // Write the line to the output file
            output.append(lineOfData);
        }
        output.close();
        input.close();
 
        // Replace the old input file with the new output file
        inputFile.delete();
        outputFile.renameTo(new File(filename));
    }

priya ana

Posts: 5
Nickname: apriyanka
Registered: Jun, 2009

Re: replace a sustring Posted: Jun 24, 2009 2:06 AM
Reply to this message Reply
Thanks so much...

priya ana

Posts: 5
Nickname: apriyanka
Registered: Jun, 2009

Re: replace a sustring Posted: Jun 25, 2009 10:30 PM
Reply to this message Reply
Hey...

i am facing a pblm with the same code..
not able to delete the existing file and rename the temp file name.
can please suggest me the reason behind this as already checked alot many things like--is file currently open,
is file is being accessed by any other function....

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: replace a sustring Posted: Jun 26, 2009 12:07 AM
Reply to this message Reply
> Hey...
>
> i am facing a pblm with the same code..
> not able to delete the existing file and rename the temp
> file name.
> can please suggest me the reason behind this as already
> checked alot many things like--is file currently open,
> is file is being accessed by any other function....

This is almost impossible to answer without access to your system. All I can do is guess.

If you know that the file is closed and is not being used by another application, check that the file is not read only and that you (or, more accurately, your application) has authority to delete the file.

I assume, from what you said above, that the application can create and write to the temporary file but cannot rename it. This suggests that the problem is access rights on the system, and not a code problem.

Flat View: This topic has 6 replies on 1 page
Topic: Needed urgent help in jsp Previous Topic   Next Topic Topic: Program to search and react to multiple websites

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use