Java列出文件夹中的所有文件


It shows that we can list all the files in a specified folder, if it exists.

[java]

  1. package com.han;  
  2.   
  3. import java.io.File;  
  4.   
  5. /** 
  6.  * It shows that we can list all the files in a specified folder, if it exists. 
  7.  * @author han 
  8.  * 
  9.  */  
  10. public class ListFiles {  
  11.   
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         File file=new File("/home/han/Documents/word");  
  15. //      System.out.println(file.getName());   
  16.         if (file.isDirectory()){  
  17.             File[] fl=file.listFiles();  
  18.             for (int i=0;i<fl.length;i++){  
  19.                 System.out.println(fl[i]);  
  20.             }  
  21.               
  22.         }  
  23.         else{  
  24.             System.out.println("File不是目录。");  
  25.         }  
  26.       
  27.     }  
  28.   
  29. }  

相关内容