How to read write this in utf-8?

How to read write this in utf-8?



I was getting an error io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence


io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence



The solution is to read and write file in UTF-8.



My code is:


InputStream input = null;
OutputStream output = null;
OutputStreamWriter bufferedWriter = new OutputStreamWriter( output, "UTF8");
input = new URL(url).openStream();
output = new FileOutputStream("DirectionResponse.xml");
byte buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;)
output.write(buffer, 0, length);

BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));
FileWriter fstream = new FileWriter("ppre_DirectionResponse.xml");
BufferedWriter out = new BufferedWriter(fstream);



I'm reading a url and writing it to a file DirectionResponse.xml. Then reading DirectionResponse.xml and writing the same as *ppre_DirecionResponse.xml* for processing.



How do I change this so that reading and writing is done in UTF-8?




2 Answers
2



First, you need to call output.close() (or at least call output.flush() before you reopen the file for input. That's probably the main cause of your problems.


output.close()


output.flush()



Then, you shouldn't use FileReader or FileWriter for this because it always uses the platform-default encoding (which is often not UTF-8). From the docs for FileReader:


FileReader


FileWriter


FileReader



The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.



You have the same problem when using a FileWriter. Replace this:


FileWriter


BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));



with something like this:


BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("DirectionResponse.xml"), "UTF-8"));



and similarly for fstream.


fstream






Could you change the code to take stream from an URL?

– Aubin
Nov 12 '12 at 20:12






Doesnt work....No suitable constructor....

– Gaurav Wadhwani
Nov 12 '12 at 20:13






@Aubin - Sure, at least if you're talking about the input stream: URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream();. Then use is as the input stream.

– Ted Hopp
Nov 12 '12 at 20:14


URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream();


is






@user905911 - I had a parenthesis in the wrong place. Try the code now.

– Ted Hopp
Nov 12 '12 at 20:15






@user905911 - I noted another problem with your code. See the first paragraph of my revised answer.

– Ted Hopp
Nov 12 '12 at 20:19



Read and Write UTF-8 File in Java



I see you are writing in utf-8 but not specifically reading in utf-8. Follow the example I've provided in the link.


try
Reader reader =
new InputStreamReader(
new FileInputStream(args[0]),"UTF-8");
BufferedReader fin = new BufferedReader(reader);
Writer writer =
new OutputStreamWriter(
new FileOutputStream(args[1]), "UTF-8");
BufferedWriter fout = new BufferedWriter(writer);
String s;
while ((s=fin.readLine())!=null)
fout.write(s);
fout.newLine();


//Remember to call close.
//calling close on a BufferedReader/BufferedWriter
// will automatically call close on its underlying stream
fin.close();
fout.close();
catch (IOException e)
e.printStackTrace();






i read that but the problem is tht i need to read a URL, those functions dont read url.

– Gaurav Wadhwani
Nov 12 '12 at 20:08






@user905911 u dindn't specify that in your question

– Aravind R. Yarram
Nov 12 '12 at 20:10






fin.close(); and fout.close(); must b called in a finally block. And since Java 7 it's better to use try-with-resources approach.

– Vladimir Vagaytsev
Jul 11 '16 at 19:24



fin.close();


fout.close();


finally



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)