java读取txt时--中文乱码的处理方法分享
下文笔者讲述java读取txt文件时,中文乱码的处理方法分享,如下所示
java读取中文文件时,出现中文乱码 此时我们需在FileInputStream中读取文件时,采用utf-8的模式 读取,即可避免中文乱码例:
InputStreamReader isr;
try {
isr = new InputStreamReader(new FileInputStream(fileUrl), "utf-8");
BufferedReader read = new BufferedReader(isr);
String s=null;
list<String> list = new ArrayList<String>();
while((s=read.readLine())!=null)
{
//System.out.println(s);
if(s.trim().length()>1){
list.add(s.trim());
}
}
System.out.println("OK!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


