FileReader亂碼

出現緣由:FileReader讀取文件的過程當中,FileReader繼承了InputStreamReader,但並無實現父類中帶字符集參數的構造函數,因此FileReader只能按系統默認的字符集來解碼,而後在UTF-8 -> GBK -> UTF-8的過程當中編碼出現損失,形成結果不能還原最初的字符。java

/**
 * 得到文件內容
 * @param file
 * @return
*/
public String getFileContent(File file){
		
		InputStreamReader reader = null;
		StringBuffer result = new StringBuffer();
		try {
			reader = new InputStreamReader(new FileInputStream(file),"gbk");
			
			int ch = reader.read();
			while(ch != -1){
				result.append((char)ch);
				ch = reader.read();
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
			reader = null;
		}
		return result.toString();
	}

 FileReader和FileWriter的使用:可用於己知輸入編碼和輸出編碼狀況下:app

public List<String> getFileContent(){
		
		List<String> list = new ArrayList<String>();
		
		File f = null;
		BufferedReader buff = null;
		BufferedWriter out = null;
                StringBuffer buffer = new StringBuffer();
		try {
			f = new File("resource/domain0108.csv");
			buff = new BufferedWriter(new FileWriter(f));
			
			String temp = buff.readLine();
			while(true){
				if(temp == null){
					break;
				}
				/*byte[] bb = temp.getBytes("utf-8");
				temp = new String(bb,"gbk");*/
				list.add(temp + "\r\n");
                                buffer.append(temp + "\r\n");
				temp = buff.readLine();
			}
			System.out.println("InfoPorcesser.getFileContent返回文件內容列表成功"+list.size());
                        out.write(buffer.toString());
                        out.flush();
                        out.close();
			buff.close();
		} catch (Exception e) {
			e.printStackTrace();
			buff = null;
		}
		
		return list;
	}                                                    
相關文章
相關標籤/搜索