java 逐行讀取txt文本如何解決中文亂碼
更新時間:2012年11月23日 15:43:17 作者:
在使用java讀取txt文本中如含有中文,可能會出現(xiàn)亂碼,很多初學(xué)者束手無策,本文將提供詳細的解決方法
java讀取txt文本中如含有中文,可能會出現(xiàn)亂碼,解決方案是:
1.要統(tǒng)一編碼,java工程的編碼,txt文本編碼,java工程中的java文本編碼都統(tǒng)一為utf-8;
2.利用 InputStreamReader(new FileInputStream(fileUrl), "utf-8")將文本再次設(shè)置為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();
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)文章
SpringBoot項目接入Nacos的實現(xiàn)步驟
SpringBoot項目使用nacos作為配置中心和服務(wù)注冊中心,同時兼容dubbo的注冊中心。 本Demo項目使用的SpringBoot版本是2.3.9.RELEASE2021-05-05利用HttpUrlConnection 上傳 接收文件的實現(xiàn)方法
下面小編就為大家?guī)硪黄肏ttpUrlConnection 上傳 接收文件的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11Spring4.0 MVC請求json數(shù)據(jù)報406錯誤的解決方法
這篇文章主要為大家詳細介紹了Spring4.0 MVC請求json數(shù)據(jù)報406錯誤的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01SpringSecurity之SecurityContextHolder使用解讀
這篇文章主要介紹了SpringSecurity之SecurityContextHolder使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java如何計算兩個時間段內(nèi)的工作日天數(shù)
這篇文章主要介紹了Java如何計算兩個時間段內(nèi)的工作日天數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07java進制轉(zhuǎn)換工具類實現(xiàn)減少參數(shù)長度
這篇文章主要為大家介紹了java進制轉(zhuǎn)換工具類實現(xiàn)減少參數(shù)長度示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02@Transactional跟@DS動態(tài)數(shù)據(jù)源注解沖突的解決
這篇文章主要介紹了@Transactional跟@DS動態(tài)數(shù)據(jù)源注解沖突的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09PowerJob的OmsLogHandler工作流程源碼解析
這篇文章主要為大家介紹了PowerJob的OmsLogHandler工作流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12