[RESOLVED] Reading and writing back UTF-8 encoded files in Java« Back to Previous Page

Hello, I have a problem with reading and then writing back UTF-8 encoded files in Java. My code is this:

// Reading input XML file
FileInputStream fstream = new FileInputStream("C:/tmp/input.xml");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

String line;
while ((line = br.readLine()) != null) {
contents.append(line).append("\n");
}

br.close();
in.close();
fstream.close();

// Processing and writing XML file
// Need to replace some tags in the file and then write it to the file
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/tmp/output.xml"), "UTF8"));
bw.write(new String(contents.toString().getBytes("UTF8")).replaceAll("__REPLACE_THIS_TAG_1__", "SOMETHING 1").replaceAll("__REPLACE_THIS_TAG_2__", "SOMETHING 2"));
bw.close();
As a result I find the resulting file to have some strange looking characters. I hope you can help me. Thanks in advance, BigDaddy
Posted by bigdaddy (Questions:1, Answers:2)
Asked on July 7, 2013 7:00 am
Category: Java
0

This is it! Thank you very much.

Posted by bigdaddy (Questions:1, Answers:2)
Answered On July 8, 2013 5:13 am #
3

Hi there, this code will do the trick for you.


// Reading
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:/tmp/input.xml"), "UTF8"));
String line;
while ((line = br.readLine()) != null) {
contents.append(line).append("\n");
}
br.close();

// Writing
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/tmp/output.xml"), "UTF8"));
bw.write(contents.toString().replaceAll("__REPLACE_THIS_TAG_1__", "SOMETHING 1").replaceAll("__REPLACE_THIS_TAG_2__", "SOMETHING 2"));
bw.close();

Hope this helps.

charlie

Posted by charlie (Questions:9, Answers:15)
Answered On July 7, 2013 9:59 pm #
No account?



It's FREE!

Like what you have found?


... and keep us going