搜索

2022-11-22


发布时间: 2022-11-24 22:42:00    浏览次数:44 次
 1 package cn.itsource._inputsteam;
 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  * @author Administrator
11  *
12  */
13 public class FileCopy {
14 
15     public static void main(String[] args) {
16         // 复制一个文件到指定目录下面
17         FileInputStream fis = null;
18         FileOutputStream fos = null;
19         try{
20             fis  = new FileInputStream("E:/好看的.txt");
21             fos = new FileOutputStream("E:/好看的-copy.txt");
22             byte[] b = new byte[1024];
23             int read = -1;  //表示每一次真正读了多少字节到byte数组
24             while ((read = fis.read(b)) != -1){    //最常用
25                 //    读一次写一次
26                 fos.write(b, 0, read);//    read表示每次读取的字节数
27                 
28             }
29         } catch (FileNotFoundException e) {
30             e.printStackTrace();
31         } catch (IOException e) {
32             e.printStackTrace();
33         } finally {
34             //关闭流
35                 try {
36                     if(fos != null){
37                     fis.close();
38                     }
39                 } catch (IOException e) {
40                     e.printStackTrace();
41                 } finally {
42                     try {
43                         //关闭流原则:先开后关
44                         if(fis != null){
45                             fis.close();
46                         }
47                         
48                     } catch (IOException e) {
49                         e.printStackTrace();
50                     }
51                 }
52             }
53         }
54         
55 
56 }

 

免责声明 2022-11-22,资源类别:文本, 浏览次数:44 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 10:42:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/puwei520/p/16916451.html