/**
* Determine the encoding format of the file
* @param fileName :file
* @return file encoding format
* @throws Exception
*/
public static String codeString(String fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
int p = (bin.read() <<
![](/images/smiles/icon_cool.gif)
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
Then, read the text
FileInputStream fInputStream = new FileInputStream(file);
//code is returned in the above method The encoding method of
InputStreamReader inputStreamReader = new InputStreamReader(fInputStream, code);
BufferedReader in = new BufferedReader(inputStreamReader);
String strTmp = "";
//Read by line
while (( strTmp = in.readLine()) != null) {
sBuffer.append(strTmp + "/n");
}
return sBuffer.toString();