SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
File file = new File("myFile");
File tmp = File.createTempFile("myFile", "tmp", file.getParentFile());
BufferedReader reader = new BufferedReader(new FileReader(file));
try
{
PrintWriter writer = new PrintWriter(new FileWriter(tmp);
try
{
String line;
while ((line = reader.readLine()) != null)
{
if (line.equals("3"))
{
line = "Name";
}
writer.println(line);
}
}
finally
{
writer.close();
}
}
finally
{
reader.close();
}
file.delete();
tmp.renameTo(file);
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
adeeb alexander wrote:This means that its not possible to just read a file using BufferedReader(new FileReader) and verify a particular line and then write the value on that line using BufferedWriter(new FileWriter).
i have to use RandomAccessFile for doing that kind of work.
It's perfectly possible to do that, but you'll have to write the complete file, and you you'll have to create a new file while doing so (and deleting the old file and renaming the new file after you're done).
Ulf Dittmer wrote:Note that RandomAccessFile can only be used if the text to be replaced is exactly as long as the text you're replacing it with - the size of the file can't change.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions