package com.xuexi.IO; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class StringBufferTest { public static void main(String[] args) { StringBuffer sb3; File file = null; FileInputStream fis= null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try{ file = new File("d:\\20156.txt"); fis = new FileInputStream(file);//创建文件输入 bis = new BufferedInputStream(fis); bos = new BufferedOutputStream( new FileOutputStream("e:\\20156.txt"));//i/o链和上面功能相同 int i=0; sb3 = new StringBuffer(); while((i=fis.read())!=-1){//返回-1表示文件读取完毕 char ch =(char)i; sb3.append(ch); } System.out.println(sb3.toString()); bos.write(sb3.toString().getBytes());//缓存满一次性写出 }catch(IOException e){ e.printStackTrace(); }finally{ try { bos.flush(); bos.close(); bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
时间: 2024-10-20 00:12:33