JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
我们继续学习File
一.文件递归
我们可以来实现一个文件管理器,简单的,但是在此之前,我们先来做点小案例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 1package com.lgl.hellojava;
2
3import java.io.File;
4
5public class HelloJJAVA {
6 public static void main(String[] args) {
7
8 File dir = new File("E:\\AndroidDelepoer");
9
10 showDir(dir);
11 }
12
13 private static void showDir(File dir) {
14 System.out.println("目录:" + dir);
15 File[] fils = dir.listFiles();
16 for (int i = 0; i < fils.length; i++) {
17 if (fils[i].isDirectory()) {
18 showDir(fils[i]);
19 } else {
20 // 列出根目录
21 System.out.println("files" + fils);
22 }
23 }
24
25 }
26}
27
28
因为目录中海油目录,只要使用同一个列出目录功能的函数完成即可,在列出过程中出现的还是目录的话,还可以再此的功能,,也就是函数自身调用自身,这种表现形式,或者手法称为递归
1
2
3
4
5 1 //递归
2 private static void method(){
3 method();
4 }
5
你可以看
1
2
3
4
5
6
7
8 1private static void toBin(int num) {
2 while (num > 0) {
3 toBin(num / 2);
4 System.out.println(num % 2);
5 // num = num / 2;
6 }
7 }
8
二.File结束练习
File讲到这里也是差不多的讲完了,感觉还不错,有很多知识点,我们就用一个小练习来结束这个知识点吧!
-
将一个指定目录下的java文件的绝对路径,存储到一个文本文件中,建立一个java文件列表的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 1package com.lgl.hellojava;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.util.ArrayList;
8
9public class HelloJJAVA {
10 public static void main(String[] args) {
11
12 /**
13 * 思路:
14 * 1.对指定的目录进行递归
15 * 2.获取递归过程所有的java文化的路径
16 * 3.将这些路径存储到集合中
17 * 4.将集合中的数据写入到一个文件中
18 */
19
20 File file = new File("f:\\");
21 java.util.List<File> list = new ArrayList<File>();
22 fileToList(file, list);
23// System.out.println(list.size());
24
25 File name = new File(file,"HelloJAVA.java");
26 writeToFile(list, name.toString());
27
28 }
29
30 public static void fileToList(File dir, java.util.List<File> list) {
31 File[] files = dir.listFiles();
32 for (File file : files) {
33 if (file.isDirectory()) {
34 fileToList(file, list);
35 } else {
36 // 判断java文件
37 if (file.getName().endsWith(".java")) {
38 list.add(file);
39 }
40 }
41 }
42 }
43
44 // 写入文件
45 public static void writeToFile(java.util.List<File> list,
46 String javaFileList) {
47 BufferedWriter bufw = null;
48 try {
49 bufw = new BufferedWriter(new FileWriter(javaFileList));
50 for (File f : list) {
51 String path = f.getAbsolutePath();
52 bufw.write(path);
53 bufw.newLine();
54 bufw.flush();
55 }
56 } catch (IOException e) {
57 try {
58 throw e;
59 } catch (IOException e1) {
60 // TODO Auto-generated catch block
61 e1.printStackTrace();
62 }
63 }
64 }
65}
66
67
三.Properties
Properties我们之前就早有接触了,他是hastable的子类,也就是说它具备map集合的特点,而且他里面存储的键值对都是字符串
- 该对象的特点,可以用于键值对形式的配置文件
这也是一个工具类,我们可以来学习学习,先来演示一下使用情况吧,我们从set,get说起
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1package com.lgl.hellojava;
2
3import java.util.Properties;
4import java.util.Set;
5
6public class HelloJJAVA {
7 public static void main(String[] args) {
8
9 SetAndGet();
10 }
11
12 // 设置和获取元素
13 private static void SetAndGet() {
14 Properties prop = new Properties();
15 prop.setProperty("张三", "20");
16
17 System.out.println(prop);
18
19 String property = prop.getProperty("张三");
20 System.out.println(property);
21
22 Set<String> stringPropertyNames = prop.stringPropertyNames();
23 for (String s : stringPropertyNames) {
24 // 打印姓名
25 System.out.println(s);
26 // 打印值
27 System.out.println(prop.getProperty(s));
28 }
29 }
30
31}
32
33
一览无余,打印的结果
四.读取配置文件
我们配置文件如果已经存在的话,我们就直接去读取了
- 我们来演示一下如何将流中的数据存储到集合中,想要通过键值对的形式保存起来
说白了就是读取本地的一个文件,然后通过键值对保存起来,我们用代码来实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 1/**
2 * 思路
3 * 1.用一个流和info.txt文件关联
4 * 2.读取遗憾数据,将该行数据进行去切割
5 * 等号左边的作为键,右边的就是值
6 */
7 private static void ReadTxt(){
8 try {
9 BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
10 String line = null;
11 Properties properties = new Properties();
12 while((line = bufr.readLine()) != null){
13 System.out.println(line);
14 String [] arr = line.split("=");
15 System.out.println(arr[0]+"...."+arr[1]);
16
17 //存
18 properties.setProperty(arr[0], arr[1]);
19 }
20 bufr.close();
21
22 System.out.println(properties);
23 } catch (FileNotFoundException e) {
24 // TODO Auto-generated catch block
25 e.printStackTrace();
26 } catch (IOException e) {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 }
31
32
这样我们输出的就是
五.load
JDK1.6以后出现的load就取代了上面的哪个方式,我们一起来实现一下吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1private static void loadDemo(){
2 try {
3 FileInputStream fish = new FileInputStream("info.txt");
4 Properties properties = new Properties();
5 //将流中的数据加载进集合
6 properties.load(fish);
7 System.out.println(properties);
8 } catch (FileNotFoundException e) {
9 e.printStackTrace();
10 } catch (IOException e) {
11 e.printStackTrace();
12 }
13 }
14
这样就可以达到效果了
六.Properties的小练习
- 用于记录应用程序运行的次数,如果使用次数已到,那么给出注册提示,这个很容易想到的是计算器,可是这个程序中,是自增,而且随着程序而存在的,如果程序退出了,这个计数也同样的消失,下一次启动程序又是从0开始了,这样不是我们想要的
我们现在要做的是程序退出之后数据任然存在而且继续自增,所以我们要创建配置文件去记录使用次数
Ok,我们用键值对的形式保存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 1package com.lgl.hellojava;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.util.Properties;
9
10public class HelloJJAVA {
11 public static void main(String[] args) {
12
13 try {
14 Properties properties = new Properties();
15 File file = new File("count.ini");
16 if (!file.exists()) {
17 file.createNewFile();
18 }
19 FileInputStream fis = new FileInputStream(file);
20
21 properties.load(fis);
22
23 int count = 0;
24 String value = properties.getProperty("time");
25
26 if (value != null) {
27 count = Integer.parseInt(value);
28 if (count >= 5) {
29 System.out.println("你余额不足呀!");
30 }
31 }
32
33 count++;
34 properties.setProperty("time", count + "");
35
36 FileOutputStream fos = new FileOutputStream(file);
37 properties.store(fos, "");
38
39 fos.close();
40 fis.close();
41
42 } catch (FileNotFoundException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 } catch (IOException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49
50 }
51
52}
53
54
我们得到的结果
OK,我们本篇就先到这里,我们下篇在聊