public static void main(String[] args)

{

//
getFile();

/*

* 需求:  对指定目录进行所有内容的列出,(包含子目录中的内容)

*

*/

File dir = new File("E:\\HB_JAVA解压");

listAll(dir, 0);

}

public static void listAll(File dir, int len)

{

System.out.println(getSpace(len) + dir.getName());

len++;

//获取指定目录下   当前的所有文件夹  或者文件对象。

File[] files = dir.listFiles();

for(File f: files)

{

if(f.isDirectory())

{

listAll(f, len);

}

else

System.out.println(getSpace(len)+f.getName());

}

}

//获取空格。。。。。。。。

public static String getSpace(int len)

{

// TODO Auto-generated method stub

StringBuilder sb = new StringBuilder();

sb.append("|--");

for(int i=0; i<len; i++)

{

sb.insert(0, "|  ");

}

return sb.toString();

}

public static void getFile()

{

/*

* 获当前目录下的文件以及文件夹的名称,包含隐藏文件。

* 调用list方法的File对象中封装的必须是目录。。。。。。。。。

* 否则会发生空指针异常。

* 如果目录存在但是没有内容,会返回一个数组,但是长度为0

*/

File file = new File("c:\\");

String[] names = file.list();

for(String name: names)

{

System.out.println(name);

}

}

====================================================关于Properties=====================

//修改Properties中的数据

public static void updateProperties() throws IOException,

FileNotFoundException

{

/*

* 对已有的   配置文件中  数据进行修改。

* 1.读取这个文件。

* 并将这个问题文件中的键值对数据存储到集合中。

* 在通过集合对数据进行修改。

* 在通过流将修改后的数据存储到文件中。

*/

//读取这个文件

File file = new File("info.txt");

if(! file.exists())

{

//如果文件不存在。

file.createNewFile();//创建文件。

}

FileReader fr = new FileReader("");

//创建集合

Properties prop = new Properties();

//将流中信息存储到集合中。

prop.load(fr);

//开始修改。。

prop.setProperty("yangchao", "19");

//开始写入

FileWriter fw = new FileWriter(file);

prop.store(fw, "info");

//关闭流

fw.close();

fr.close();

}

//读取Properties中的数据

public static void PropertiesReader() throws FileNotFoundException,

IOException

{

/*

* 集合中的数据来自于一个文件。

* 必须要保证文件汇总的数据是键值对。

* 需要使用到读取流

*/

Properties prop = new Properties();

FileInputStream fis = new FileInputStream("");

//使用load方法。

prop.load(fis);

prop.list(System.out);

}

//将Properties中的信息   写入到硬盘上

public static void PropertiesWriter() throws FileNotFoundException,

IOException

{

Properties prop = new Properties();

//存储元素:

prop.setProperty("yangchao", "24");

prop.setProperty("xiaoxueqiang","23");

prop.setProperty("yangchunwang", "20");

prop.setProperty("renwei", "22");

//想要将这些集合中的字符串键值信息持久化存储在硬盘上

//需要相关    关联输出流

FileOutputStream fos = new FileOutputStream("info.txt");

//将集合中给的数据存储到文件中使用store方法

prop.store(fos, "info");

fos.close();

}

----------------------------------------应用********************2014/05/26 10:40

/*

* 定义功能,获取一个应用程序运行的次数,如果超过5次,给出使用次数已到,  请注册的提示。 并不要运行该程序。

*

* 思路:

* 1.应该有计数器。每次程序启动都需要计数一次。并且是在原有的基础次数上进行计数

* 2.计数器就是i一个变量,程序启动时就应该进行计数,计数器 就应该存在于内存中,进行运算。程序结束  计数器  消失。;

* 程序一结束,计数器就消失,在此启动程序,计数器又重新初始化。

* 而我们需要多次启动同意个应用程序,使用的是同一个计数器。

* 就需要吧计数器的声明周期边长,从内存中到硬盘上。

* 3.如何使用这个计数器?


首先程序启动时,应该先读取这个用于记录计数器信息的配置文件。


获取上一次计数器值。并进行判断。。。。。。。


器次对计数器的值进行自增。并自增后的次数重新存储到配置文件中。

*/

public class PropertiesTest2

{

public static void getAppCount() throws IOException

{

//将配置文件封装成File对象.

File confile = new File("conunt.properties");

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("使用次数已到,  请注册!!!");

throw new RuntimeException("没给钱");

}

}

count++;

//将改变后的次数  重新存储到集合中

prop.setProperty("time", count+"");

//存储完后   开始写到 文件中

FileOutputStream fos = new FileOutputStream(confile);

prop.store(fos, "info");

fos.close();

fis.close();

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. PHP优化小建议
  2. C++ 系列:C++ 基础 002
  3. Android 四大组件之再论service
  4. elasticsearch2
  5. K Smallest Sums
  6. 【POJ 3279 Fliptile】开关问题,模拟
  7. label中添加图片
  8. Spring_Spring与AOP_AspectJ基于注解的AOP实现
  9. SpringCloud学习之Hystrix
  10. Luogu P3731 [HAOI2017]新型城市化
  11. LwIP Application Developers Manual6---Application API layers
  12. UI控件Telerik UI for WinForms发布R1 2019|附下载
  13. python-函数参数不固定
  14. 摩拜单车模式优于OFO双向通信才能被认可
  15. Linux命令_用户和用户组管理
  16. Winform 出现“Win已停止工作”解决方法
  17. 使用django的admin的后台管理界面
  18. 0.前言 three.js 简介
  19. Spring零散所得
  20. HDU 1114 Piggy-Bank (dp)

热门文章

  1. xlistview的XML(脚)xlistview_footer
  2. 嵌入式 -- WINKHUB 边信道攻击 (NAND Glitch)
  3. 关于binary search的一点解惑
  4. 前端开发者应该知道的 CSS 小技巧
  5. &quot;由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断&quot;的解决方案
  6. Unity3D ShaderLab 语法:Properties
  7. 怎么书写高质量jQuery代码
  8. 第一个Sprint冲刺第一天
  9. 暴力枚举——Help Me with the Game
  10. fsutil