简单的解决了读取前判断TXT的编码问题……
// 显示打开文件的内容
void readFile(String path)
{
try
{
File file = new File(path);
InputStream fs = new FileInputStream(file);
BufferedReader br ;
// byte[] buffer = new byte[fs.available()];
byte[] buffer = new byte[3];
fs.read(buffer);
fs.close();
String str = "";
int size = (int) file.length();
int charRead = 0;
char[] content = new char[size];
// byte[] content = new byte[size];
//判断文件的编码
if (buffer[0] == -17 && buffer[1] == -69 && buffer[2] == -65)
{
br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "utf-8"));// 编码转换
while (br.ready())
{
charRead += br.read(content, charRead, size - charRead);
}
br.close();
str = new String(content, 0, charRead);
text.setText(str);
} else
{
br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gbk"));// 编码转换
while (br.ready())
{
charRead += br.read(content, charRead, size - charRead);
}
br.close();
str = new String(content, 0, charRead);
text.setText(str);
}
} catch (IOException e)
{
System.out.println("读取文件出错");
}
}