大世界——小人物

导航

Java——File(文件)


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();

}

}

 

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

posted on 2014-07-07 10:01  大世界——小人物  阅读(139)  评论(0编辑  收藏  举报