首页 > 编程语言 > 详细

Java学习之IO流三

时间:2017-07-21 22:42:19      阅读:794      评论:0      收藏:0      [点我收藏+]

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         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
74         //定义字节输出流缓冲流对象
75         BufferedOutputStream bos  = new BufferedOutputStream(new FileOutputStream(dest));
76         //定义直接缓冲数组
77         byte[] buf = new byte[1024];
78         //定义读取长度
79         int len = 0;
80         while(-1 != (len = bis.read(buf))) {
81             bos.write(buf, 0, len);
82         }
83         bos.flush();
84         
85         //释放资源
86         bos.close();
87         bis.close();
88     }
89 }

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         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
51         //定义字节输出流缓冲流对象
52         BufferedOutputStream bos  = new BufferedOutputStream(new FileOutputStream(dest));
53         //定义直接缓冲数组
54         byte[] buf = new byte[1024];
55         //定义读取长度
56         int len = 0;
57         while(-1 != (len = bis.read(buf))) {
58             bos.write(buf, 0, len);
59         }
60         bos.flush();
61         
62         //释放资源
63         bos.close();
64         bis.close();
65     }
66 }

3.  用代码实现以下需求

(1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)

 

 1 /**
 2  * 2. 用代码实现以下需求
 3     (1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
 4     (2)打印格式:
 5         to=3
 6         think=1
 7         you=2
 8         //........
 9     (3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
10  * @author vanguard
11  *
12  */
13 public class Demo04 {
14     public static void main(String[] args) throws IOException {
15         String str = "If you want to change your fate I think you must come to the dark horse to learn java";
16         //将字符串分割成字符串数组
17         String[] arrays = str.split(" ");
18         //定义存放单词和个数的Map集合,单词为键,个数为值
19         Map<String, Integer> letters = new HashMap<String, Integer>();
20         //遍历Map集合,统计单词的个数
21         for(String arr : arrays) {
22             if(!letters.containsKey(arr)) {
23                 letters.put(arr, 1);
24             } else {
25                 letters.put(arr, letters.get(arr) + 1);
26             }
27         }
28         //遍历Map集合打印输出
29         for(String letter : letters.keySet()) {
30             System.out.println(letter + "=" + letters.get(letter));
31         }
32         //定义字符缓冲输出流
33         BufferedWriter bw = new BufferedWriter(new FileWriter("count.txt"));
34         //遍历Map集合,写数据
35         for(String letter : letters.keySet()) {
36             bw.write(letter + "=" + letters.get(letter));
37             bw.newLine();
38             bw.flush();
39         }
40         //释放资源
41         bw.close();
42     }
43 }

 

4.java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号

 1 /**
 2  * 4.java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号
 3  * @author vanguard
 4  *
 5  */
 6 public class Demo05 {
 7     public static void main(String[] args) throws IOException {
 8         //定义计算行号的变量
 9         int lineNumber = 0;
10         //定义字符缓冲输入流
11         BufferedReader br = new BufferedReader(new FileReader("count.txt"));
12         //定义读取一行内容的字符串
13         String line = null;
14         //读取一行,读取null时,跳出循环
15         while(null != (line = br.readLine())) {
16             //行号加1
17             lineNumber++;
18             //控制台输出行号和读取的字符串
19             System.out.println(lineNumber + "  " + line);
20         }
21         br.close();
22     }
23 }

 

Java学习之IO流三

原文:http://www.cnblogs.com/guodong-wang/p/7219787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!