黑马程序员------IO流(No.3)(File类、Properties类)

来源:互联网 发布:techrules 知乎 编辑:程序博客网 时间:2024/06/03 16:44

---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ---------------------- 

 

微笑File类

用来将文件或文件夹封装成对象

方便对文件与文件夹的属性信息进行操作

File对象可以作为参数传递给流的构造函数

 

import java.io.*;class FileDemo{public static void main(String[] args)throws Exception{ consMethod();}public static void consMethod()throws Exception{//将a.txt封装成File对象,可以将已有的和未出现的文件或文件夹封装成对象。File f1 = new File("a.txt");//File f2 = new File("C://abc","b.txt");File d = new File("c://abc");File f3 = new File(d,"c.txt");// static String 变量separator是分隔符,自动识别系统的分隔符。有利于跨平台。File f4 = new File("C:"+File.separator+"abc"+File.separator+"das"+File.separator+"d.txt");}public static void sop(Object obj){System.out.println(obj);}}


微笑File类的常见方法:

 

1.创建。

boolean createNewFiel()在指定位置创建文件,如果该文件已经存在,则不创建,返回false。和输出流不一样,输出流对象一建立就会创建文件;而且文件如果已经存在,会覆盖。

boolean mkdir()创建文件夹

boolean mkdirs()创建多级文件夹

public static void method_2(){File f = new File("file.txt");//创建文件夹File dir = new File("dasda//dsafg//gsdd//dasd//");sop(dir.mkdirs());}


 

2.删除

boolean delete()删除失败返回false。如果文件正在被使用,则删除不了返回false

void deleteOnExit() 在程序退出时删除指定文件

 

3.判断

boolean exists() 文件是否存在

isFile()文件是否是标准文件

isDriectory()是否是目录

isHidden()是否隐藏

isAbsolute()是否是绝对路径



 

4.获取信息 

getName()得到名字

getPath()得到路径

gtParent()得到

getAbsolutePath()

long lastModified()

long length()

 

public static void method_2(){File f = new File("file.txt");sop(f.getPath());//返回new File()中传入的路径sop(f.getAbsolutePath());//返回绝对路径//该方法返回的是绝对路径中的父目录。如果获得的是相对路径则返回null;如果相对路径中有上一层目录,那么该目录就是返回结果sop(f.getParent());}


 遍历根目录,以及某个目录下的文件名:

 

import java.io.*;class FileDemo2{public static void main(String[] args){listRoot();listDemo();}public static void listRoot(){//listRoots为静态方法,返回File数组File[] files = File.listRoots();for(File f : files){System.out.println(f);}}public static void listDemo(){File f = new File("c://");//得到c盘中的文件,该目录必须存在String[] names = f.list();for(String name:names){System.out.println(name);}}}


 遍历某个目录中的某种格式的文件

public static void guoLv(){//这里指定要过滤的文件所在的目录File dir = new File("C:\\Users\\Xiaobi\\Desktop");//使用内部类进行过滤String[] arr = dir.list(new FilenameFilter(){public boolean accept(File dir,String name){return name.endsWith(".txt");}});System.out.println(arr.length);//增强for循环,输出结果for(String s:arr){System.out.println(s);}}


列出指定目录下文件或者文件夹,包含子目录中的内容。也就是列出指定目录下的所有内容因为目录中还有目录,

只要使用同一列出目录功能的函数完成即可。在列出过程中出现的还是目录的话,还可以再次调用本功能。也就

是函数自身调用自身,这种表现形式叫做递归。

 

递归要注意的条件:

1.限定条件

2.要注意递归的次数。尽量避免内存溢出。

import java.io.*;class  FileDemo3{public static void main(String[] args){showDir(new File("C:\\Users\\Xiaobi\\Desktop\\Test"));}public static void showDir(File dir){System.out.println(dir+"........");File[] files = dir.listFiles();for(File f : files){if(f.isFile()){System.out.println(f);}else{showDir(f);}}}}

扩展,将某目录下各层的.java文件都得到,并且输出到文本文件中

import java.io.*;class  FileDemo3{public static void main(String[] args)throws Exception{//设置输出设备System.setOut(new PrintStream("hahh.txt"));showDir(new File("C:\\Users\\Xiaobi\\Desktop\\自学"));}public static void showDir(File dir)throws Exception{System.out.println(dir+"........");File[] files = dir.listFiles();for(File f : files){if(f.isFile()){//得到所以.java文件if(f.getName().endsWith(".java"))System.out.println(f);}else{showDir(f);}}}}




 微笑Properties

 

Properties是hashtable的子类。

也就是说它具有map集合的特点,而且它里面存储的键值对都是字符串。

是集合中和IO技术相结合的集合容器。

 

该对象的特点:

可以用于键值对形式的配置文件。

用一个流和prop.txt文件关联

读取一行数据,将该行数据用“=”进行切割

等号左边作为链,右边作为值。存入到Peoperties集合中

/*Properties是hashtable的子类。也就是说它具有map集合的特点,而且它里面存储的键值对都是字符串。是集合中和IO技术相结合的集合容器。该对象的特点:可以用于键值对形式的配置文件。用一个流和prop.txt文件关联读取一行数据,将该行数据用“=”进行切割等号左边作为链,右边作为值。存入到Peoperties集合中*/import java.io.*;import java.util.*;class PropDemo{public static void main(String[] args)throws IOException{//setAndGet();userProp();}//Properties的各功能实现public static void userProp()throws IOException{Properties prop = new Properties();prop.load(new FileReader("prop.txt"));//修改值prop.setProperty("wangwu","45");//存入原配置文件,持久化prop.store(new FileWriter("prop.txt"), "that is cool");prop.list(System.out);}//Properties的load方法的原理模拟public static void setAndGet()throws IOException{//用一个流和prop.txt文件关联FileReader fis = new FileReader("prop.txt");BufferedReader br = new BufferedReader(fis);String s = null;Properties prop = new Properties();//读取一行数据,将该行数据用“=”进行切割while((s=br.readLine())!=null){String[] a = s.split("=");//等号左边作为链,右边作为值。存入到Peoperties集合中prop.setProperty(a[0],a[1]);}//修改值prop.setProperty("wangwu","37");//存入原配置文件,持久化prop.store(new FileWriter("prop.txt"), "hava  a  try");prop.list(System.out);br.close();}}



 

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ---------------------- 

详情请查看:http://edu.csdn.net

 

原创粉丝点击