读取各种编码的text文件

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);
}

你可能感兴趣的:(java)