package IOliu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class BufferedInputStreamTest { public static void main(String[] args) { StringBuffer sb = null; File file = null; File file2 = null; FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //带缓存的字节输入流 file = new File("D:\\user.txt"); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); //带缓存的字节输出流 file2 = new File("D:\\2016.txt"); fos = new FileOutputStream(file2); bos = new BufferedOutputStream(fos); int i = 0; sb = new StringBuffer(); try { while((i = bis.read())!=-1){ char ch = (char)i; sb.append(ch); bos.write(ch); } System.out.println(sb.toString()); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ try { bos.flush(); fis.close(); fos.close(); bis.close(); bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
时间: 2024-10-06 06:09:26