package com.chenyang.nse.bussiness.commmon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class EncodingDetect {
   public static void main(String[] args) {
      String file = "E:/PWCP_ANM_ARQC_S99_EAIR_ANM_L88_P9_20100126150002424.xml";
      String encode = getJavaEncode(file);
      System.out.println(encode);
      readFile(file, encode);
   }

   public static String getJavaEncode(String filePath) {
      BytesEncodingDetect s = new BytesEncodingDetect();
      String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(new File(filePath))];
      return fileCode;
   }

   public static void readFile(String file, String code) {
      try {
         String myCode = code != null && !"".equals(code) ? code : "UTF8";
         InputStreamReader read = new InputStreamReader(new FileInputStream(file), myCode);
         BufferedReader fr = new BufferedReader(read);
         String line = null;

         for(int flag = 1; (line = fr.readLine()) != null && line.trim().length() > 0; System.out.println(line)) {
            if (flag == 1) {
               line = line.substring(1);
               ++flag;
            }
         }

         fr.close();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

   }
}
