Enrique San Martín Enrique San Martín - 11 months ago 69
Java Question

java: how to convert a file to utf8

i have a file that have some non-utf8 caracters (like "ISO-8859-1"), and so i want to convert that file (or read) to UTF8 encoding, how i can do it?

The code it's like this:

File file = new File("some_file_with_non_utf8_characters.txt");

/* some code to convert the file to an utf8 file */


edit: Put an encoding example

Answer Source
  String charset = "ISO-8859-1"; // or what corresponds
  BufferedReader in = new BufferedReader( 
      new InputStreamReader (new FileInputStream(file), charset));
  String line;
  while( (line = in.readLine()) != null) { 

There you have the text decoded. You can write it, by the simmetric Writer/OutputStream methods, with the encoding you prefer (eg UTF-8).