J
jrefactors
If I want to read and replace text in a file called file.txt, I
should need to write it to a temporary file first, and then
delete file.txt, and then rename the temporary file to file.txt??
Here's the code fragment:
BufferedReader br = new BufferedReader(new FileReader(fileName));
PrintWriter out = new PrintWriter(new FileWriter(fileName + ".out"));
String line;
while ((line = br.readLine()) != null)
{ if (line.indexOf("XXX") != -1)
line = line.replaceFirst("XXX",replacement);
out.println(line);
}
please advise, thanks!!
should need to write it to a temporary file first, and then
delete file.txt, and then rename the temporary file to file.txt??
Here's the code fragment:
BufferedReader br = new BufferedReader(new FileReader(fileName));
PrintWriter out = new PrintWriter(new FileWriter(fileName + ".out"));
String line;
while ((line = br.readLine()) != null)
{ if (line.indexOf("XXX") != -1)
line = line.replaceFirst("XXX",replacement);
out.println(line);
}
please advise, thanks!!