P
Prabh
Hello all,
I'm rather new to Java and was wondering if there is a more Java-esque
way of doing the following.
I need to update a particular line of a file, foo.txt keeping all the
other lines exactly the same.
I am thinking of :
1) Copy foo.txt to foo.txt.bak.
2) Use FileReader and LineNumberReader to read each line in the .bak.
3) Use PrintWriter, pw.print to print each of the above line to the
original.
a) With no changes, if line doesnt match the required pattern.
b) With changes, if line matches the pattern.
I realize this is a clunky-shell scripting way of doing things.
Is there a more Java-esque way of doing this?
Could I bypass the "backup the original file" part?
Is there a way I can read and write the same file?
Read the original file in some temp stream, process stream
line-by-line, write /w or w/o changes to the same file.
What Input/Output streams should I be looking at?
Thanks,
Prabh
I'm rather new to Java and was wondering if there is a more Java-esque
way of doing the following.
I need to update a particular line of a file, foo.txt keeping all the
other lines exactly the same.
I am thinking of :
1) Copy foo.txt to foo.txt.bak.
2) Use FileReader and LineNumberReader to read each line in the .bak.
3) Use PrintWriter, pw.print to print each of the above line to the
original.
a) With no changes, if line doesnt match the required pattern.
b) With changes, if line matches the pattern.
I realize this is a clunky-shell scripting way of doing things.
Is there a more Java-esque way of doing this?
Could I bypass the "backup the original file" part?
Is there a way I can read and write the same file?
Read the original file in some temp stream, process stream
line-by-line, write /w or w/o changes to the same file.
What Input/Output streams should I be looking at?
Thanks,
Prabh