java 读txt中文乱码

public void readyFile(){
String strFile="";
FileReader in=null;
try{
String strLine="";
in=new FileReader(getPath);
buf=new BufferedReader(in);
strLine=buf.readLine();
while(strLine!=null){
strFile+=strLine;
strLine=buf.readLine();
}
}catch(IOException e1){
e1.printStackTrace();
}
strbFile=new StringBuffer(strFile);
fileOver=false;
}

public void readFile(){
String strPart=""; int n=320;
if(strbFile.length()<=n){
jta1.setText(strbFile+"\n");
fileOver=true;
return;
}
strPart=strbFile.substring(0,n);
strbFile=strbFile.delete(0,n);
jta1.setText(strPart+"\n");
}

txt文档一般是GBK编码格式的,你可以将它另存一下,改为Utf-8格式,你也可以将你的开发工具调成GBK编码格式,总之就是将你的开发环境的编码格式与你要读取的txt文件的编码格式调成一致的。或者此处你可以使用java的转换流,指定编码格式去读取txt文件,就是使用InputStreamReader(new FileInputStream(“filepath”),"GBK"),如果还是乱码,你就再换编码格式,不是UTF-8就是GBK
温馨提示:答案为网友推荐,仅供参考
相似回答