Java中utf-8 bom文件的输出,读入。utf-8的判断等sample source

// add bom to file
FileOutputStream os = new FileOutputStream(file);
os.write(0xef);
os.write(0xbb);
os.write(0xbf);

//sample source

public static void main(String arg[]) {
    // カレントディレクトリのパスを取得します。
    String dir = System.getProperty("user.dir");
 
    // 出力したいファイル名を指定します。
    File file = new File(dir + "/test.txt");
    PrintWriter pw = null;
 
    try {
        FileOutputStream os = new FileOutputStream(file);
        os.write(0xef);
        os.write(0xbb);
        os.write(0xbf);
 
        pw = new PrintWriter(new OutputStreamWriter(os));
 
        pw.println("ひらがな");
        pw.println("カタカナ");
        System.out.println("ファイルの書き込みに成功しました!");
    } catch (IOException e) {
        System.out.println(e);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
}


//先頭がBOMの場合スキップする方法
//対処法は色々考えられますが、ここでは、BufferedInputStreamを被せ、 先頭がBOMの場合スキップし、BOMでない場合先頭まで巻き戻す形を 示します。

import java.io.*;
public class Test1 {
   /** テキストファイルを読み込み用にオープンする */
   public static BufferedReader  openTextFileR(
                        String fileName
                       ,String charSet
                       )throws Exception{
      return new BufferedReader(
                 new InputStreamReader(
                     skipUTF8BOM(
                        new FileInputStream(
                            new File(fileName))
                       ,charSet)
                    ,charSet));
      }
   /** UTF-8のBOMをスキップする */
   public static InputStream skipUTF8BOM(
                        InputStream is
                       ,String      charSet
                        )throws Exception{
      if( !charSet.toUpperCase().equals("UTF-8") ) return is;
      if( !is.markSupported() ){
         // マーク機能が無い場合BufferedInputStreamを被せる
         is= new BufferedInputStream(is);
         }
      is.mark(3); // 先頭にマークを付ける
      if( is.available()>=3 ){
         byte b[]={0,0,0};
         is.read(b,0,3);
         if( b[0]!=(byte)0xEF ||
             b[1]!=(byte)0xBB ||
             b[2]!=(byte)0xBF ){
            is.reset();// BOMでない場合は先頭まで巻き戻す
            }
         }
      return is;
      }
   public static void main(String[] args_){
      try{
         String         line;
         BufferedReader br;
         // BOM処理なし
         br = new BufferedReader(new InputStreamReader(
                                    new FileInputStream(
                                         new File(args_[0]))
                                   ,"utf-8"));
         while( (line=br.readLine())!=null ) System.out.println(line);
         br= new BufferedReader(new InputStreamReader(
                                    new FileInputStream(
                                         new File(args_[1]))
                                   ,"utf-8"));
         while( (line=br.readLine())!=null ) System.out.println(line);
         // BOM処理あり
         br= openTextFileR(args_[0],"utf-8");
         while( (line=br.readLine())!=null ) System.out.println(line);
         br= openTextFileR(args_[1],"utf-8");
         while( (line=br.readLine())!=null ) System.out.println(line);
         }
      catch(Exception e){
         e.printStackTrace(System.err);
         }
      }
   }
   
//
public static boolean isUTF8(byte[] src)
    {
        try {
            byte[] tmp = new String(src, "UTF-8").getBytes("UTF-8");
            return Arrays.equals(tmp, src);
        }
        catch(UnsupportedEncodingException e) {
            return false;
        }
    }

posted on 2017-02-14 10:51  sdfczyx  阅读(223)  评论(0)    收藏  举报