欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 逐行讀取txt文本如何解決中文亂碼

 更新時(shí)間:2012年11月23日 15:43:17   作者:  
在使用java讀取txt文本中如含有中文,可能會(huì)出現(xiàn)亂碼,很多初學(xué)者束手無策,本文將提供詳細(xì)的解決方法
java讀取txt文本中如含有中文,可能會(huì)出現(xiàn)亂碼,解決方案是:
1.要統(tǒng)一編碼,java工程的編碼,txt文本編碼,java工程中的java文本編碼都統(tǒng)一為utf-8;
2.利用 InputStreamReader(new FileInputStream(fileUrl), "utf-8")將文本再次設(shè)置為utf-8
3.具體代碼如下
復(fù)制代碼 代碼如下:

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();

相關(guān)文章

最新評(píng)論