/**

* 一:.IO学习一:
* 1.java中的java.io.File类表示一个目录(文件夹)或者一个文件。
*   java.io包 → 通过数据流、序列化、和文件系统提供系统输入和输出。
*/
/**
*  2.File表示一个文件。
*/
//写法一:
File file1 = new File("c:\\a.txt");
//写法二:
File file2 = new File("c:/a.txt");
//写法三:
File file3 = new File("c:"+File.separator+"a.txt");
/**
* 3.常用的方法:
* isFile() → 是否为文件。
* isDirectory → 是否为目录。
* canread() → 是否可读。
* canwrite() → 是否可写。
* length() → 字节大小。
* listFiles() → 当前目录下的所有文件和目录。
* (返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
* getName():返回由此抽象路径名表示的文件或目录的名称。
* getPath():将此抽象路径名转换为一个路径名字的字符串)
* delete() → 删除文件。
* exists() → 是否存在。
* createNewFile() → 创建新文件。
* lastModified() → 最后一次被修改的时间。(单位为:ms)
* list() → 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
*/
/**
* 4.File作为一个目录
*/
File file4 = new File("c:/p_w_picpaths");
if(!file4.exists()) {
//创建一个目录
file4.mkdir();
}
File file5 = new File("c:/p_w_picpaths1/happy");
if(!file5.exists()) {
//创建带有层次的文件夹c:/p_w_picpaths/happy
file5.mkdirs();
}
/**
* 5.区别一下list()和listFiles()方法
*/
File file6 = new File("c:/p_w_picpaths1");
String[] arrayFile6 = file6.list();
for(String s : arrayFile6) {
System.out.println(s);
}
File[] arrayFile61 = file6.listFiles();
for(File f : arrayFile61) {
System.out.println(f.getName());
System.out.println(f.getPath());
}
//再举个例子:
File file7 = new File("c:/windows");
File[] files = file7.listFiles();
for(File f : files) {
if(f.isFile())  {
//如果是文件
System.out.println("文件名为:"+f.getName()+"\t路径名为:"+f.getPath());
} else {
//如果是目录
System.out.println("目录名为:["+f.getName()+"]\t路径名为:"+f.getPath());
}
}
/**
* 递归算法的一个应用
*/
//digui("c:/windows");
show("c:/p_w_picpaths1");
show("c:/windows");


----------------------------------------------------------

以上是在main方法中的 ,下面是一个递归算法的方法。

/**
* 递归算法
* @param path 路径名,如:c:/p_w_picpaths
*/
public static void show(String path) {
File file = new File(path);
File[] files = file.listFiles();
for(File f : files) {
if(f.isFile()) {
//如果f是文件
System.out.println("--文件名--:"+f.getName());
} else {
//如果f是目录
if(f.getPath() != null ) {
//如果该目录存在,继续递归
show(f.getPath());
} else {
System.out.println("该目录不存在!-----------");
}
}
}
}
 
上面的方法还可以写成下面的形式:
(注意一点:上面的方法是有bug的,和下面一比,你就知道了!!思考一下哦)
 
  1. /** 
  2.  * 输出一个目录下所有的文件和文件夹 
  3.  * @param path 
  4.  */ 
  5. public static void showAllList(String path) { 
  6.     System.out.println("目录为:---" + path + "---"); 
  7.      
  8.     File file = new File(path); 
  9.      
  10.     if(file.isFile()) { 
  11.         //如果是一个文件目录 
  12.         System.out.println("文件路径为:" + file.getPath() + "文件名为:" + file.getName()); 
  13.     } else if(file.isDirectory()) { 
  14.         //如果是一个目录 
  15.         File[] fileList = file.listFiles(); 
  16.          
  17.         if(fileList != null) { 
  18.              
  19.             for (File f : fileList) { 
  20.                 if(f.isDirectory()) { 
  21.                     System.out.println("目录:【"+f.getPath()+"】"); 
  22.                     //调用showAllList()方法,相当于递归算法了!! 
  23.                     showAllList(f.getPath()); 
  24.                 } else { 
  25.                     //表示是一个文件,不需要递归了 
  26.                     System.out.println("文件路径为:" + file.getPath() + "文件名为:" + file.getName()); 
  27.                 } 
  28.             } 
  29.              
  30.         } else { 
  31.             System.out.println("xxx"); 
  32.         } 
  33.  
  34.     } else { 
  35.         //如果既不是目录也不是文件 
  36.         System.out.println("sorry,O(∩_∩)O~"); 
  37.     }