日期:2014-05-20 浏览次数:20998 次
public static String getFileContent(String path) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(path));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
return sb.toString();
}
------解决方案--------------------
public String getEncoding(String path){
//ANSI 0 0 0 utf8 -17 -69 -65 UNICODE -1 -2 0
String code="";
code="gb2312";
try {
InputStream inputStream=new FileInputStream(new File(path));
byte []head=new byte[3];
inputStream.read(head);
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[0]==-26&&head[1]==-95)){
code="UTF-8";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(code);
return code;
}