0306作业

使用FileInoutStream和FileOutputStream实现复制粘贴

 1 package com.kgc.lx.io;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 /**
 9  * 先读再写,相当于复制
10  */
11 public class Copy {
12     public static void main(String[] args){
13         FileInputStream fis=null;
14         FileOutputStream fos=null;
15 
16         try {
17             fis=new FileInputStream("c:\\kkk\\kkk.txt");
18             fos=new FileOutputStream("c:\\kkk\\Copy.txt");
19             System.out.println("文件正在复制,文件大小"+fis.available()+"字节");
20             int len=-1;
21             String str="";
22             while((len=fis.read())!=-1){
23                 str+=(char)len;
24             }
25             byte[] b=str.getBytes();
26             fos.write(b,0,b.length);
27             System.out.println("文件粘贴成功");
28             fos.flush();
29         } catch (FileNotFoundException e) {
30             e.printStackTrace();
31         } catch (IOException e) {
32             e.printStackTrace();
33         }finally {
34             try {
35                 if (fos!=null){
36                     fos.close();
37                 }if(fis!=null){
38                     fis.close();
39                 }
40 
41             } catch (IOException e) {
42                 e.printStackTrace();
43             }
44         }
45     }
46 }

运行结果

文件目录 

 

posted @ 2019-03-06 16:49  纯属丶简单  阅读(165)  评论(0编辑  收藏  举报