javaseday23_02(深度遍历 递归 Properties 存储读取配置文件 注册有效时间 还有<>尖括号 复杂配置)

来源:互联网 发布:花生壳 该域名被锁定 编辑:程序博客网 时间:2024/06/12 20:07
/* * 需求 对指定目录进行所有内容的列出(包含子目录中的内容) * 也可以理解为深度遍历 */public class FileTest01 {    public static void main(String[] args) {        File dir = new File("e:\\");// 需要不包含系统的 没有权限访问 返回空 有空指针异常        listAll(dir, 0);    }    // listFiles 有路径和名称 更多操作    private static void listAll(File dir, int level) {        // 获取指定目录下当前的所有文件夹或者文件对象        System.out.println(getSpace(level) + "dir" + dir.getAbsolutePath());        level++;// 如果直接在方法内部定义level 会导致 一个level 通用 还没弹栈就 又使用 应该传入一个level        File[] files = dir.listFiles();        // 如果不对数组角标进行操作 增强for循环就行        for (int x = 0; x < files.length; x++) {            if (files[x].isDirectory()) {// 往深层遍历                listAll(files[x], level);// 自己调用自己的方法 递归            }            System.out.println(files[x].getAbsolutePath());        }    }    private static String getSpace(int level) {        StringBuilder sb = new StringBuilder();        sb.append("|--");// 碰到文件才加--        for (int i = 0; i < level; i++) {            sb.insert(0, "| ");// 要插入到前面才能看到效果        }        return sb.toString();    }}


/*
* 递归
* 函数自身直接或者间接的调用了自身
* 一个功能在被重复使用 并每次使用时 参与运算的结果和上一次调用有关
* 这时可以用递归来解决问题
* 注意 递归一定要明确条件 否则容易栈溢出 只进不出
* 注意一下递归的次数 太多报异常 栈溢出
*/
public static void main(String[] args) {
// toBin(11);
System.out.println(getSum(90000));
}
private static int getSum(int num) {
if(num==1)
return 1;
return num+getSum(num-1);
}
private static void toBin(int num) {
if(num>0){
toBin(num/2);//放在下面 就先打印 在上面要完成递归
System.out.println(num%2);
}
}
public static void show(){
show();//直接递归
method();//间接递归
}
public static void method(){
show();
}

/*
* 删除一个带内容的目录
*
* 原理 必须从最里面往外删 windows 也是如此 删一个大文件 很快 但是 文件夹还要读条
* 需要深度遍历
*
*/
public class RemoveDirTest01 {
public static void main(String[] args) {
File dir = new File(“d:\Test”);
removeDir(dir);
}

private static void removeDir(File dir) {
File[] files = dir.listFiles();
for(File file : files){
if (file.isDirectory()){
removeDir(file);
}else{
System.out.println(file+”:”+file.delete());
}//如果返回false 则删除失败或者删除多次

}
System.out.println(dir+”:”+dir.delete());

}
}

public static void main(String[] args) throws IOException {
/*
* Map
* Hashtable
* Properties
* Properties集合
* 特点
* 1、该集合中的键和值都是字符串类型
* 2、集合中的数据可以保存到流中 或者从流中获取数据
* 和流相结合
* 该集合通常用于操作以键值对形式存在的配置文件
* enumeration <> propertyNames 1.6之前版本使用 现在有更强的set<> stringPropertyNames
* 持久化 就是存的时间长点 断了电还在 比如放在硬盘里面
*/
// propertiesDemo();
// propertiesDemo2();
// Properties prop = System.getProperties();
// prop.list(System.out);//按照键值排列 不用换行了
// propertiesDemo3();
// propertiesDemo_4();
testDemo_1();
}
//对已有的配置文件中的信息进行修改
/*
* 读取这个文件
* 并将这个文件中的键值数据存储到集合中
* 再通过集合对数据进行修改(各司其职 集合修改 流获取)
* 再通过流将修改后的数据存储到文件中
*/
public static void testDemo_1() throws IOException {
//读取这个文件
File file = new File(“info.txt”);//可能不存在 先封装成对象进行判断
if(!file.exists()){//保证文件一定存在
file.createNewFile();
}
FileReader fr = new FileReader(“info.txt”);//如果把FileWriter放它下面会导致文件重新写入成为空白后继续操作
//创建集合存储配置信息
Properties prop = new Properties();
//将流中信息存储到集合中
prop.load(fr);
prop.setProperty(“王武”, “11”);//没存进去 要通过流
FileWriter fw = new FileWriter(file);//全部重新写一边
prop.store(fw, “name + age”);
prop.list(System.out);
fw.close();
fr.close();
}
//模拟load方法
public static void myLoad() throws IOException{
Properties prop = new Properties();
BufferedReader br = new BufferedReader(new FileReader(“info.txt”));
String line = null;
while((line = br.readLine())!=null){
if(line.startsWith(“#”))
continue;
String[] arr = line.split(“=”);
// System.out.println(arr[0]+”::”+arr[1]);
prop.setProperty(arr[0],arr[1]);
}
prop.list(System.out);
br.close();
}
private static void propertiesDemo_4() throws IOException {
Properties prop = new Properties();
//集合中的数据来自于一个文件
//注意 必须要保证该文件中的数据是键值对
//需要使用到读取流
FileInputStream fis = new FileInputStream(“info.txt”);
//使用load 方法
prop.load(fis);
prop.list(System.out);
fis.close();//开流 一定要关流
}
private static void propertiesDemo3() throws IOException {
Properties prop = new Properties();
prop.setProperty(“张三”, “10”);
prop.setProperty(“李四”, “11”);
prop.setProperty(“李个”, “12”);
prop.setProperty(“王武”, “13”);
//想要将这些集合中的字符串键值信息持久化存储到文件中
//需要关联输出流
FileOutputStream fos = new FileOutputStream(“info.txt”);//用FileWriter也行 因为 是键值对信息 都文本
prop.store(fos, “name+age”);//放中文信息的话要被不能识别中文的码表编写 没有的话就用 十六进制的 \uxxx 建议使用英文
fos.close();
}
/**
* 演示Properties 集合和流对象相结合的功能
* PrintStream 字节流 就OutputStream 意思差不多 Print意思就是打印 输出
*
*/
private static void propertiesDemo2() {
Properties prop = new Properties();
prop.setProperty(“张三”, “10”);
prop.setProperty(“李四”, “11”);
prop.setProperty(“李个”, “12”);
prop.setProperty(“王武”, “13”);
prop.setProperty(“大小”, “8”);
prop.list(System.out);//调试用的 不能修改
}

/*
* Properties集合的存和取
*/
public static void propertiesDemo(){
//创建一个Properties集合
Properties prop = new Properties();
//存储元素
prop.setProperty(“张三”, “10”);
prop.setProperty(“李四”, “11”);
prop.setProperty(“李个”, “12”);
prop.setProperty(“王武”, “13”);
prop.setProperty(“大小”, “8”);
//修改元素
prop.setProperty(“大小”, “25”);
//取出所有元素

Set names = prop.stringPropertyNames();
for(String name:names){
String value = prop.getProperty(name);
System.out.println(name+”:”+value);
}
}

/*
* 定义功能 获取一个应用程序运行的次数 如果超过5次 给出使用次数已到请注册的提示并不要再运行程序
*
* 思路
* 1、应该有计数器
* 每次程序启动都需要计数一次 并且是在原有的次数上进行技术
* 2、计数器就是一个变量 突然冒出一想法 程序启动时进行技术 计数器就必须存在于内存中并进行运算
* 可是程序一结束 计数器消失了 那么再次启动该程序 计数器又重新被初始化
* 而我们需要多次启动同一个应用程序 使用的是同一个计数器
* 这就需要计数器的生命周期变长 从内存存储到硬盘中
* 3、如何使用这个计数器呢
* 首先 程序启动时 应该先读取这个用于记录计数器信息的配置文件
* 获取上一次计数器次数 并进行试用次数的判断
* 其次 对该次数进行自增 并将自增后的次数重新存储到配置文件中
* 4、文件中的信息该如何进行存储并体现
* 直接存储次数值可以 但是不明确该数据的含义 所以起名字就变得很重要 更复杂的就用XML文件
* 这就有了名字和值的对应 所以可以使用键值对
* 可用映射关系map集合搞定 又需要读取硬盘上的数据 所以map+io = Properties
*/
/*
* 只能对数据起名称
* name =zhangsan;
* age = 20;
* 复杂配置 可以对数据进行描述
*
*
* zhangsan
* 20
*
*
* zhangsan
* 20
*
*
*
*/
public class PropertiesDemo02 {
public static void main(String[] args) throws IOException {
getAppCount();
}
//标签就尖括号 泛型 描述信息
//次数仅仅是配置信息中的一种 应用很广 还能记录你个性化设置
public static void getAppCount() throws IOException{
//将配置文件封装成File 对象//java 中配置文件的后缀名 properties
File confile = new File(“count.properties”);//ini 在windows 和常见应用软件
if(!confile.exists()){
confile.createNewFile();
}
FileInputStream fis = new FileInputStream(confile);
Properties prop = new Properties();
prop.load(fis);
//从集合中 通过键获取次数
String value = prop.getProperty(“time”);
//定义计数器 记录获取到的次数
int count = 0;
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
// System.out.println(“使用次数已到 请注册购买正版”);
// return;
throw new RuntimeException(“使用次数已到”);//别随便抛error 虚拟机底层抛的
}
}
count++;
//将改变后的次数重新存储到集合中
prop.setProperty(“time”, count+”“);
FileOutputStream fos = new FileOutputStream(confile);//重新创建后覆盖写入

prop.store(fos,”“);
fos.close();
fis.close();

}
}

阅读全文
0 0