Tuesday, April 1, 2014

How to Read and Write UTF-8 File in Java?

The InputStreamReader and the OutputStreamWriter support UTF-8 character encoding. Here is example code:
public class Program {
    
    public static void main(String... args)  {
        
        if (args.length != 2) {
            return ;
        }

        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();
        }

    }
}
You can run it:
c:\>java Program inputfilename outputfilename


Also, you can use Scanner which is provided by Java 5.0 to read UTF-8 file.

No comments:

Post a Comment