欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读取各种编码的text文件 博客分类: java  

程序员文章站 2024-02-25 20:21:57
...
public static void main(String[] args) throws IOException {
File file = new File("d:/java思考Unicode big endian.txt");
InputStream inputStream = new FileInputStream(file);  
        byte[] head = new byte[3];  
        inputStream.read(head);   
        String code = "gb2312";  
        if (head[0] == -1 && head[1] == -2 )
        {
        code = "UTF-16";  
        }
        if (head[0] == -2 && head[1] == -1 )
        {
        code = "Unicode";  
        }
        if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
        {
        code = "UTF-8";  
        }
        System.out.println(code); 
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file), code));
String str="";
String r=br.readLine();
while(r!=null){
str+=r+"\r";
r=br.readLine();
}
System.out.println(str);
}