• java读取utf8类型的文件 _ Java认证 _ 考试大
  • 发布时间:2012-10-26 17:03 | 作者: | 来源: | 浏览:
  •     源文件为utf-8格式,且里面包含中文,如:
       
        in ('133','153','180','189') then '电信'
       
        in ('130','131','132','145','155','156','183','185','186') then '联通'
       
        1,不加转码的时候中文会乱码:
       
        Java代码
       
        1.public static void main(String[] args) throws Exception {
       
        2.    File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
       
        3.    InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));
       
        4.    BufferedReader bufReader = new BufferedReader(insReader);
       
        5.    String line = new String();
       
        6.    while ((line = bufReader.readLine()) != null) {
       
        7.        System.out.println(line);
       
        8.    }
       
        9.    bufReader.close();
       
        10.}
       
        public static void main(String[] args) throws Exception {
       
        File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
       
        InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));
       
        BufferedReader bufReader = new BufferedReader(insReader);
       
        String line = new String();
       
        while ((line = bufReader.readLine()) != null) {
       
        System.out.println(line);
       
        }
       
        bufReader.close();
       
        }
       
        Java代码
       
        1.in ('133','153','180','189') then '鐢典俊'
       
        2.in ('130','131','132','145','155','156','183','185','186') then '鑱旈?'
       
        in ('133','153','180','189') then '鐢典俊'
       
        in ('130','131','132','145','155','156','183','185','186') then '鑱旈?'
       
        2,如果对string进行utf-8编码,某些中文加单引号会导致乱码:
       
        Java代码
       
        1.public static void main(String[] args) throws Exception {
       
        2.        //String charsetName = "UTF-8";
       
        3.        File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
       
        4.        InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));
       
        5.        BufferedReader bufReader = new BufferedReader(insReader);
       
        6.        String line = new String();
       
        7.        while ((line = bufReader.readLine()) != null) {
       
        8.            System.out.println(new String(line.getBytes(),"utf-8"));
       
        9.        }
       
        10.        bufReader.close();
       
        11.    }
       
        public static void main(String[] args) throws Exception {
       
        //String charsetName = "UTF-8";
       
        File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
       
        InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));
       
        BufferedReader bufReader = new BufferedReader(insReader);
       
        String line = new String();
       
        while ((line = bufReader.readLine()) != null) {
       
        System.out.println(new String(line.getBytes(),"utf-8"));
       
        }
       
        bufReader.close();
       
        }

        相关推荐:

        JAVA对象转JSON格式的字符串

        Java中Uuid的实现

        java类数组初始化

  • 收藏 | 打印
  • 相关内容