Java InputStreamReader create with Charset

Description

Java InputStreamReader create with Charset

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {

   public static void main(String[] args)throws Exception {

      InputStream is = new FileInputStream("sjis.txt");
      InputStreamReader reader = null;
      StringBuilder sb = new StringBuilder();
      if (is != null) {
         reader = new InputStreamReader(is, Charset.forName("SJIS"));
         int ch = reader.read();
         while (ch != -1) {
            sb.append((char) ch);
            ch = reader.read();/*from  ww  w.j  a  v  a2  s  .c  o  m*/
         }
         reader.close();
      }
      System.out.println(sb.toString());
   }
}



PreviousNext

Related