Java学习之IO流二

1.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

 1 /**
 2  * 1.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
 3  * @author vanguard
 4  *
 5  */
 6 public class Demo01 {
 7     public static void main(String[] args) {
 8         //键盘输入两个文件夹路径
 9         Scanner sc = new Scanner(System.in);
10         System.out.println("请输入源目录:");
11         String srcPath = sc.next();
12         System.out.println("请输入目标目录:");
13         String destPath = sc.next();
14         File src = new File(srcPath);
15         File dest = new File(destPath);
16         try {
17             copyDir(src, dest);
18         } catch (FileNotFoundException e) {
19             System.out.println("源文件不存在");
20             e.printStackTrace();
21         } catch (IOException e) {
22             System.out.println("读取文件失败");
23             e.printStackTrace();
24         }
25         
26     }
27     /**
28      * 复制文件夹
29      * @param src
30      * @param dest
31      * @throws IOException
32      */
33     private static void copyDir(File src, File dest) throws IOException {
34         //遍历源文件夹
35         for(File file : src.listFiles()) {
36             //如果是文件夹,调用复制文件夹细节方法
37             if(file.isDirectory()) {
38                 copyDirDetail(file, dest);
39             } else {
40                 //否则,调用复制文件夹方法
41                 copyFile(file, new File(dest, file.getName()));
42             }
43         }
44     }
45     /**
46      * 复制文件夹细节
47      * @param src
48      * @param dest
49      * @throws IOException
50      */
51     private static void copyDirDetail(File src, File dest) throws IOException {
52             dest = new File(dest, src.getName());
53             //目标文件夹下创建文件夹
54             dest.mkdirs();
55             //遍历文件夹下文件及文件夹
56             for(File file : src.listFiles()) {
57                 if(file.isDirectory()) {
58                     copyDirDetail(file, dest);
59                 } else {
60                     copyFile(file, new File(dest, file.getName()));
61                 }
62             }
63     }
64     
65     /**
66      * 复制文件
67      * @param src
68      * @param dest
69      * @throws IOException
70      */
71     private static void copyFile(File src, File dest) throws IOException {
72         //定义字节输入、输出流
73         FileInputStream fis = new FileInputStream(src);
74         FileOutputStream fos  = new FileOutputStream(dest);
75         //定义直接缓冲数组
76         byte[] buf = new byte[1024];
77         //定义读取长度
78         int len = 0;
79         while(-1 != (len = fis.read(buf))) {
80             fos.write(buf, 0, len);
81         }
82         fos.flush();
83         
84         //释放资源
85         fos.close();
86         fis.close();
87     }
88 }

2.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录

 1 /**
 2  * 2.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录
 3  * @author vanguard
 4  *
 5  */
 6 public class Demo02 {
 7     public static void main(String[] args) {
 8         File src = new File("d:\TortoiseSVN");
 9         File dest = new File("d:\txt");
10         //定义文件个数
11         int count = 0;
12         try {
13             count = fileNum(src, dest, count);
14         } catch (IOException e) {
15             
16             e.printStackTrace();
17         }
18         System.out.println("txt文件的个数为:" + count);
19     }
20     
21     /**
22      * 统计指定目录及子目录下所有txt文件的个数
23      * @param src
24      * @throws IOException 
25      */
26     private static int fileNum(File src, File dest, int count) throws IOException {
27         //遍历目录下的文件及目录
28         for(File f : src.listFiles()) {
29             if(f.isDirectory()) {
30                 count = fileNum(f, dest, count);
31             } else {
32                 //如果是文件,且以.txt结尾,文件个数加1,调用复制文件方法
33                 if(f.getName().endsWith(".txt")) {
34                     count++;
35                     copyFile(f, new File(dest, f.getName()));
36                 }
37             }
38         }
39         return count;
40     }
41 
42     /**
43      * 复制文件
44      * @param src
45      * @param dest
46      * @throws IOException
47      */
48     private static void copyFile(File src, File dest) throws IOException {
49         //定义字节输入、输出流
50         FileInputStream fis = new FileInputStream(src);
51         FileOutputStream fos  = new FileOutputStream(dest);
52         //定义直接缓冲数组
53         byte[] buf = new byte[1024];
54         //定义读取长度
55         int len = 0;
56         while(-1 != (len = fis.read(buf))) {
57             fos.write(buf, 0, len);
58         }
59         fos.flush();
60         
61         //释放资源
62         fos.close();
63         fis.close();
64     }
65 }

3.键盘输入10个数,放到数组中

(1)去除该数组中大于10的数
(2)将该数组中的数字写入到本地文件number.txt中

 1 /**
 2  * 3.键盘输入10个数,放到数组中
 3     (1)去除该数组中大于10的数
 4     (2)将该数组中的数字写入到本地文件number.txt中
 5  * @author vanguard
 6  *
 7  */
 8 public class Demo03 {
 9     public static void main(String[] args) {
10         int count = 0;
11         Scanner sc = new Scanner(System.in);
12         int[] arr = new int[10];
13         for(int i = 0; i < 10; i++) {
14             arr[i] = sc.nextInt();
15             if(arr[i] > 10) {
16                 count++;
17             }
18         }
19         int[] arr2 = new int[arr.length - count];
20         count = 0;
21         //遍历数组,去除大于10的数
22         for(int i = 0; i < arr.length; i++) {
23             if(arr[i] < 10) {
24                 arr2[count++] = arr[i];
25             }
26         }
27         System.out.println(Arrays.toString(arr2));
28         //向本地文件写入数组中的数组
29         FileWriter fw = null;
30         try {
31             fw = new FileWriter("number.txt");
32             for(int num : arr2) {
33                 fw.write(num +"
");
34             }
35             fw.flush();
36         } catch (FileNotFoundException e) {
37             System.out.println("目的目录找不到");
38             e.printStackTrace();
39         } catch (IOException e) {
40             System.out.println("文件写入失败");
41             e.printStackTrace();
42         } finally {
43             try {
44                 if(fw != null) 
45                     fw.close();
46             } catch (IOException e) {
47                 System.out.println("释放资源失败");
48                 e.printStackTrace();
49             }
50         }
51     }
52 }

4.产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
(2)把数组中的数字放到当前文件夹的number.txt文件中

 1 /**
 2  * 4.产生10个1-100的随机数,并放到一个数组中
 3     (1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
 4     (2)把数组中的数字放到当前文件夹的number.txt文件中
 5  * @author vanguard
 6  *
 7  */
 8 public class Demo04 {
 9     public static void main(String[] args) {
10         Random r = new Random();
11         int[] arr = new int[10];
12         List<Integer> list = new ArrayList<Integer>();
13         //产生10个1-100的随机数
14         for(int i = 0; i < arr.length; i++) {
15             arr[i] = r.nextInt(101);
16         }
17         //遍历数组,将数组中大于等于10的数组放到list集合中
18         for(int num : arr) {
19             if(num >= 10) {
20                 list.add(num);
21             }
22         }
23         //遍历输出list集合中的数组
24         for(int num : list) {
25             System.out.print(num + " ");
26         }
27         FileWriter fw = null;
28         try {
29             fw = new FileWriter("number.txt");
30             for(int num : arr) {
31                 fw.write(num +"
");
32             }
33             fw.flush();
34         } catch (FileNotFoundException e) {
35             System.out.println("目的目录找不到");
36             e.printStackTrace();
37         } catch (IOException e) {
38             System.out.println("文件写入失败");
39             e.printStackTrace();
40         } finally {
41             try {
42                 if(fw != null) 
43                     fw.close();
44             } catch (IOException e) {
45                 System.out.println("释放资源失败");
46                 e.printStackTrace();
47             }
48         }
49     }
50 }

5.从控制台获取输入的文件目录然后将该目录(包含子目录)下的.java文件复制到D:/java文件夹中

 1 /**
 2  * 5.从控制台获取输入的文件目录然后将该目录(包含子目录)
 3  * 下的.java文件复制到D:/java文件夹中
 4  * @author vanguard
 5  *
 6  */
 7 public class Demo05 {
 8     public static void main(String[] args) {
 9         System.out.println("请输入一个文件目录:");
10         String srcPath = new Scanner(System.in).next();
11         String destPath = "d:\Java";
12         File src = new File(srcPath);
13         File dest = new File(destPath);
14         try {
15             listFile(src, dest);
16         } catch (IOException e) {
17             
18             e.printStackTrace();
19         }
20         
21     }
22     /**
23      * 遍历目录下的子目录及文件
24      * @param src
25      * @param dest
26      * @throws IOException
27      */
28     private static void listFile(File src, File dest) throws IOException {
29         //遍历目录下的文件及目录
30         for(File f : src.listFiles()) {
31             if(f.isDirectory()) {
32                 listFile(f, dest);
33             } else {
34                 //如果是文件,且以.txt结尾,文件个数加1,调用复制文件方法
35                 if(f.getName().endsWith(".java")) {
36                     copyFile(f, new File(dest, f.getName()));
37                 }
38             }
39         }
40     }
41     /**
42      * 复制文件
43      * @param src
44      * @param dest
45      * @throws IOException
46      */
47     private static void copyFile(File src, File dest) throws IOException {
48         //定义字节输入、输出流
49         FileInputStream fis = new FileInputStream(src);
50         FileOutputStream fos  = new FileOutputStream(dest);
51         //定义直接缓冲数组
52         byte[] buf = new byte[1024];
53         //定义读取长度
54         int len = 0;
55         while(-1 != (len = fis.read(buf))) {
56             fos.write(buf, 0, len);
57         }
58         fos.flush();
59         
60         //释放资源
61         fos.close();
62         fis.close();
63     }
64 }
原文地址:https://www.cnblogs.com/guodong-wang/p/7219769.html