Java讀取TXT文本文件亂碼解決方案.doc_第1頁
Java讀取TXT文本文件亂碼解決方案.doc_第2頁
Java讀取TXT文本文件亂碼解決方案.doc_第3頁
Java讀取TXT文本文件亂碼解決方案.doc_第4頁
Java讀取TXT文本文件亂碼解決方案.doc_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

Java讀取TXT文本文件亂碼解決方案今天在解析一個文本文件的時候出現(xiàn)了亂碼,以前從未遇到,花了點時間解決了,在此總結(jié)一下:首先,先看一下解析的代碼:private String getProjectData(File file)String data = null;try BufferedReader br = new BufferedReader(new FileReader(file);String str = br.readLine();data = str.split(t);br.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();return data;這個方法很簡單,就是獲取txt文件中的一行數(shù)據(jù),然后轉(zhuǎn)換為數(shù)組,我這個文本中只有一行。就這種情況下獲取到的str是亂碼,我想出現(xiàn)亂碼肯定是字符集編碼的問題,然后自己新建了一個文本文檔測試沒有問題(開始用的文本時別的程序?qū)С龅模?,所以我估計是開始那個txt文件編碼的問題,查看發(fā)現(xiàn)是Unicode編碼(如何查看txt的編碼呢,教你一個笨笨的辦法:打開文件另存為,然后看到最下面的編碼,默認選擇的就是當(dāng)前文檔的編碼格式),問題就出現(xiàn)在這里。問題找到了就好解決了,修改代碼為:private String getProjectData(File file)String data = null;try BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),UTF-16);String str = br.readLine();data = str.split(t);br.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();return data;使用文件流讀取,轉(zhuǎn)換編碼為“UTF-16”,這樣亂碼問題就解決了。歸納一下:txt文本文檔有四種編碼選項:ANSI、Unicode、Unicode big endian、UTF-8;默認應(yīng)該是ANSI選項,就是系統(tǒng)的默認編碼,一般是GBK,這種情況下用第一段代碼解析是沒有問題的,也不需要轉(zhuǎn)碼;其他格式就需要用第二段代碼對應(yīng)轉(zhuǎn)碼了,Unicode對應(yīng)UTF-16,UTF-8就不用說了。關(guān)于獲取txt文件編碼,我們可能有時候需要用程序獲取,動態(tài)判斷,這里給一點資料,參考參考:ANSI: 無格式定義Unicode: 前兩個字節(jié)為FFFE Unicode文檔以0xFFFE開頭Unicode big endian: 前兩字節(jié)為FEFFUTF-8: 前兩字節(jié)為EFBB UTF-8以0xEFBBBF開頭用程序取出前幾個字節(jié)并進行判斷即可。package com.syscan.oa.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ReadWritTxtFileIO public static void main(String args) /ReadDate();/WriteDate();String path = d:/file4.txt;String str = writeToDat(path);System.out.println(str.length = +str.length);for (int i = 0; i str.length; i+) System.out.println(stri);String splitStr = stri.split(#);System.out.println(splitStr.length = +splitStr.length);for(int j = 0; j splitStr.length; j+)System.out.println(splitStrj);/* * 讀取數(shù)據(jù) */public static void ReadDate() String url = e:/2.txt;try FileReader read = new FileReader(new File(url);StringBuffer sb = new StringBuffer();char ch = new char1024;int d = read.read(ch);while (d != -1) String str = new String(ch, 0, d);sb.append(str);d = read.read(ch);System.out.print(sb.toString(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();/* * 寫入數(shù)據(jù) */SuppressWarnings(unchecked)public static void WriteDate() try File file = new File(D:/abc.txt);if (file.exists() file.delete();file.createNewFile();BufferedWriter output = new BufferedWriter(new FileWriter(file);ArrayList ResolveList = new ArrayList();for (int i = 0; i 10; i+) ResolveList.add(Math.random() * 100);for (int i = 0; i ResolveList.size(); i+) output.write(String.valueOf(ResolveList.get(i) + n);output.close(); catch (Exception ex) System.out.println(ex);SuppressWarnings(unchecked)public static String writeToDat(String path) File file = new File(path);List list = new ArrayList();String str = null;try BufferedReader bw = new BufferedReader(new FileReader(file);String line = null;/ 因為不知道有幾行數(shù)據(jù),所以先存入list集合中while (line = bw.readLine() != null) list.add(line);bw.close()

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論