The encoding chain

UTF-8 charset is normally used as default in all the components of a Java software system from database to web browser.  Refer these links in order to get information about it.

However, external files can be served by other systems in any charset different to UTF-8. To preserve encoding chain, file reading must be done specifying external charset in these terms.

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestEncoding {

  public static void processFile(String filename, String charset) throws IOException {

    FileInputStream fstreamIn = new FileInputStream(filename);
    DataInputStream in = new DataInputStream(fstreamIn);

    // Specify charset for the InputStream
    BufferedReader br = new BufferedReader(
        new InputStreamReader(in, charset));

    String strLine;
    while ((strLine = br.readLine()) != null)   {
      // Do something with the line: insert into database, ...
    }

    fstreamIn.close();

  }

  public static void main(String...strings) throws IOException {

    String filename = "/hosts/data.input";
    String charset = "ISO-8859-15";

    processFile(filename, charset);

  }

}

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s