jdk有一个关于UTF-8的bug
所以加了一句 br.skip(1);
bugID:
http://bugs.java.com/view_bug.do?bug_id=4508058
1 public static void main(String[] args) { 2 StringBuffer buffer=new StringBuffer(); 3 try { 4 FileInputStream fis=new FileInputStream("test.txt"); 5 InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); 6 BufferedReader br=new BufferedReader(isr); 7 String line=null; 8 br.skip(1); 9 while ((line=br.readLine())!=null) { 10 buffer.append(line); 11 buffer.append("\r\n"); 12 } 13 buffer.delete(buffer.length()-2,buffer.length()); 14 br.close(); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 System.out.println(buffer); 19 try { 20 FileOutputStream fos=new FileOutputStream("test1.txt"); 21 OutputStreamWriter osw=new OutputStreamWriter(fos); 22 osw.write(buffer.toString()); 23 osw.flush(); 24 osw.close(); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 }
时间: 2024-10-19 08:31:18