How to read write this in utf-8?

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

The solution is to read and write the 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 read the url and write it to the DirectionResponse.xml file. Then read DirectionResponse.xml and write the same as * ppre_DirecionResponse.xml * for processing.

How to change this so that reading and writing are done in UTF-8?

+7
source share
2 answers

First you need to call output.close() (or at least call output.flush() ) before opening the file again for input. This is probably the main cause of your problems.

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

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

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

 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 .

+22
source

Reading and writing a UTF-8 file in Java

I see that you are writing in utf-8, but not specifically reading in utf-8. Follow the example 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(); } 
+2
source

All Articles