凉凉
楼主
发布于 2023-6-2 13:19:33
阅读 1220
查看全部
java读取txt文本中如含有中文,可能会呈现乱码,处置方案是:
1.要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8;
2.利用 InputStreamReader(new FileInputStream(fileUrl), "utf-8")将文本再次设置为utf-8
3.详细代码如下
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); |
|