文件拷贝——高淇JAVA300讲笔记之IO和File

案例一:定义一个拷贝文件的方法

 1 package com.bjsxt.io.byteIO;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 public class CopyFileDemo {
12 
13     public static void main(String[] args) {
14         String src = "E:/xp/test/1.jpg";
15         String dest = "e:/xp/test/4.jpg";
16         try {
17             copyFile(src,dest);
18         } catch (FileNotFoundException e) {
19             e.printStackTrace();
20             System.out.println("文件不存在");
21         } catch (IOException e) {
22             e.printStackTrace();
23             System.out.println("拷贝文件失败|关闭流失败");
24         }
25     }
26     
27     /**
28      * 文件的拷贝
29      * @param 源文件路径
30      * @param 目录文件路径
31      * @throws FileNotFoundException,IOException
32      * @return 
33      */
34     public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
35         //1、建立联系 源(存在且为文件) + 目的地(文件可以不存在)
36         File src = new File(srcPath);
37         File dest = new File(destPath);
38         if(!src.isFile()) {  //不是文件或者为null
39             System.out.println("只能拷贝文件");
40             throw new IOException("只能拷贝文件");
41         }
42         //2、选择流
43         InputStream is = new FileInputStream(src);
44         OutputStream os = new FileOutputStream(dest);
45         //3、文件拷贝 循环+读取+写出
46         byte[] flush = new byte[1024];
47         int len = 0;
48         //读取
49         while(-1!=(len=is.read(flush))) {
50             //写出
51             os.write(flush, 0, len);
52         }
53         os.flush();  //强制刷出
54         
55         //关闭流(先打开的后关闭)
56         os.close();
57         is.close();
58     }
59 }

 

案例二:把拷贝文件的方法写进一个工具类里,并重载了一遍,参数既可以传路径,也可以传File对象

 1 package com.bjsxt.io.byteIO;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 /**
12  * 文件造作
13  * 1、拷贝
14  *
15  */
16 public class FileUtil {
17     /**
18      * 文件的拷贝
19      * @param 源文件路径
20      * @param 目录文件路径
21      * @throws FileNotFoundException,IOException
22      * @return 
23      */
24     public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
25         //1、建立联系 源(存在且为文件) + 目的地(文件可以不存在)
26         copyFile(new File(srcPath),new File(destPath));
27     }
28     /**
29      * 文件的拷贝
30      * @param 源文件File对象
31      * @param 目录文件File对象
32      * @throws FileNotFoundException,IOException
33      * @return 
34      */
35     public static void copyFile(File src,File dest) throws FileNotFoundException,IOException {
36         if(!src.isFile()) {  //不是文件或者为null
37             System.out.println("只能拷贝文件");
38             throw new IOException("只能拷贝文件");
39         }
40         //2、选择流
41         InputStream is = new FileInputStream(src);
42         OutputStream os = new FileOutputStream(dest);
43         //3、文件拷贝 循环+读取+写出
44         byte[] flush = new byte[1024];
45         int len = 0;
46         //读取
47         while(-1!=(len=is.read(flush))) {
48             //写出
49             os.write(flush, 0, len);
50         }
51         os.flush();  //强制刷出
52         
53         //关闭流(先打开的后关闭)
54         os.close();
55         is.close();
56     }
57     
58     
59 }

 

posted on 2018-02-08 22:54  爱游泳的小飞象  阅读(164)  评论(0编辑  收藏  举报

导航