bufferdeoutputwriter
程序代码:
public class BufferedReader_demo01 { public static void main(String[] args) { File file = new File("C:\\Users\\郭赛\\Desktop\\demo03.txt"); FileInputStream fis = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; FileOutputStream fos = null; OutputStreamWriter osr = null; try { /** * 建立字节流和字符流的关系 */ fis = new FileInputStream(file); inputStreamReader = new InputStreamReader(fis); fos = new FileOutputStream(new File("C:\\Users\\郭赛\\Desktop\\demo04.txt")); osr = new OutputStreamWriter(fos); char[] bytes = new char[1024]; int a; while((a = inputStreamReader.read(bytes)) != -1) osr.write(bytes,0,a); bufferedReader = new BufferedReader(inputStreamReader); String encoding = inputStreamReader.getEncoding(); System.out.println(encoding); String s; while ((s = bufferedReader.readLine()) != null) System.out.println(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(bufferedReader == null) bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } try { if(inputStreamReader == null) inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } try { if(fis == null) fis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(osr == null) osr.close(); } catch (IOException e) { e.printStackTrace(); } try { if(fos == null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
为什么每次使用这个方法 while((a = inputStreamReader.read(bytes)) != -1)
osr.write(bytes,0,a);
文件中的内容就会消失