K
Kevin Kirkeby
I've looked through many of the postings in this forum and the Java
Tutorial, and I have not really been able to find an answer to this
problem. I need to parse a file for a value and replace it with another
value. It would be really slick if there were a class/method that allows
me to do this; however, I have not had any luck finding one. I have
written the following class below that does this. What it does is look
for the the value to be changed and then writes the data (and changed
strings) out to a different file. So, this works but does anyone know if:
1) There is a class/method that does this already?
2) Is it possible to write directly back to the file that I'm parsing?
The code below writes all the output to a new file and then I'll have to
copy it back. (As a result, the authorities are all different from the
original now.)
3) Is there a simpler approach than what I have done below?
4) If below is the only way to do this, would you recommend using
something other than BufferedReader/Writer? Any other suggestions on
making the code better?
Thanks,
Kevin
public class Parser {
public static void replaceString(String str, String newStr)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader("C:\\temp\\my.xml");
FileWriter fw = new FileWriter("C:\\temp\\myNew.xml");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
// Look for the string specified by "str"
String line;
int index;
String newLine;
while ((line = br.readLine()) != null) {
if ((index = line.indexOf(str)) != -1) {
// String was found.
newLine = line.substring(0, index)
+ newStr
+ line.substring(index + str.length());
System.out.println("New line: " + newLine);
} else {
newLine = line;
}
bw.write(newLine);
bw.newLine();
}
bw.flush();
bw.close();
}
public static void main(String[] args) {
try {
replaceString("true", "false");
} catch (Exception e) {
System.out.println("Exception in replaceString. " + e.toString());
}
}
}
Tutorial, and I have not really been able to find an answer to this
problem. I need to parse a file for a value and replace it with another
value. It would be really slick if there were a class/method that allows
me to do this; however, I have not had any luck finding one. I have
written the following class below that does this. What it does is look
for the the value to be changed and then writes the data (and changed
strings) out to a different file. So, this works but does anyone know if:
1) There is a class/method that does this already?
2) Is it possible to write directly back to the file that I'm parsing?
The code below writes all the output to a new file and then I'll have to
copy it back. (As a result, the authorities are all different from the
original now.)
3) Is there a simpler approach than what I have done below?
4) If below is the only way to do this, would you recommend using
something other than BufferedReader/Writer? Any other suggestions on
making the code better?
Thanks,
Kevin
public class Parser {
public static void replaceString(String str, String newStr)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader("C:\\temp\\my.xml");
FileWriter fw = new FileWriter("C:\\temp\\myNew.xml");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
// Look for the string specified by "str"
String line;
int index;
String newLine;
while ((line = br.readLine()) != null) {
if ((index = line.indexOf(str)) != -1) {
// String was found.
newLine = line.substring(0, index)
+ newStr
+ line.substring(index + str.length());
System.out.println("New line: " + newLine);
} else {
newLine = line;
}
bw.write(newLine);
bw.newLine();
}
bw.flush();
bw.close();
}
public static void main(String[] args) {
try {
replaceString("true", "false");
} catch (Exception e) {
System.out.println("Exception in replaceString. " + e.toString());
}
}
}