java I/O---复制文本文件

JAVA学习网 2017-12-31 06:00:08

利用FileInputStreamFileOutputStream 复制文本

  1 public class CopyTextByBuffer {
  2
  3 	/**
  4 	 * @param args
  5 	 */
  6 	public static void main(String[] args) {
  7 		// TODO Auto-generated method stub
  8 		FileInputStream fis = null;
  9 		FileOutputStream fos = null;
 10 		try {
 11 			fis = new FileInputStream("H:\\workspace\\Testfile\\1.txt");
 12 			fos = new FileOutputStream("H:\\workspace\\Testfile\\1-copybybuffer.txt");
 13
 14 			//定义缓冲区
 15 			byte [] buf = new byte[1024];
 16 			int len = 0;
 17
 18 			//将fis存入buf
 19 			while((len=fis.read(buf))!=-1){
 20 				fos.write(buf,0,len);
 21 			}
 22 		} catch (IOException e) {
 23 			// TODO: handle exception
 24 		}finally{
 25 			if(fos != null){
 26 				try {
 27 					fos.close();
 28 				} catch (IOException e) {
 29 					// TODO Auto-generated catch block
 30 					e.printStackTrace();
 31 				}
 32 			}
 33 			if(fis != null){
 34 				try {
 35 					fis.close();
 36 				} catch (IOException e) {
 37 					// TODO Auto-generated catch block
 38 					e.printStackTrace();
 39 				}
 40 			}
 41 		}
 42 	}
 43
 44 }
阅读(708) 评论(0)