日期:2014-05-20  浏览次数:20893 次

java BufferedReader 读取文件 乱码,求解
Java code

File file = new File("D:/aa.cer");
InputStreamReader isr;

isr = new InputStreamReader(new FileInputStream(file),"utf-8");
String line=null;
while((line=bf.readLine())!=null){
    System.out.print(line);
}

编码设置过还是乱码,求解

------解决方案--------------------
Java code
File file = new File("D:/aa.cer");
        FileReader fr=null;

        try {
            fr = new FileReader(file);
            char[] temp=new char[(int) file.length()];
            fr.read(temp);
            fr.close();
            System.out.println(new String(temp));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

------解决方案--------------------
你确定你的D:/aa.cer文件时utf-8编码的吗?

------解决方案--------------------
探讨
不知道文件是什么编码的?这样要怎么解决的

------解决方案--------------------
可以测试文件的编码,然后根据编码来打开文件的:http://blog.csdn.net/wula0010/article/details/6862696
------解决方案--------------------
分别调用下面的两个方法,看看输出什么

Java code


public static void readGBKCodingFile(String fileName) {//读取GBK文件的内容
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    fileName), "gbk"));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
            }
        }
    }
    
    
    public static void readUTFCodingFile(String fileName) {//读取UTF-8文件的内容
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    fileName), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
            }
        }
    }

------解决方案--------------------

你想转化文件格式,试试下面的两个方法
Java code


    public static void convertGBKFileToUTFFile(String srcFileName, String destFileName) {//把GBK文件转换为UTF文件
        BufferedReader br = null;
        ;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    srcFileName), "gbk"));
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(destFileName), "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null)
                    br.close();
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
            }
        }
    }
    
    public static void convertUTFFileToGBKFile(String srcFileName, String destFileName) {//把GBK文件转换为UTF文件
        BufferedReader br = null;
        ;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    srcFileName), "utf-8"));
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(destFileName), "gbk"));
            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null)
                    br.close();
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
            }
        }
    }