出现原因:FileReader读取文件的过程中,FileReader继承了InputStreamReader,但并没有实现父类中带字符集参数的构造函数,所以FileReader只能按系统默认的字符集来解码,然后在UTF-8 -> GBK -> UTF-8的过程中编码出现损失,造成结果不能还原最初的字符。
/** * 获得文件内容 * @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的使用:可用于己知输入编码和输出编码情况下:
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 + " "); buffer.append(temp + " "); 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; }