`

运用IO流知识,编一个程序,将指定目录下所有文件遍历出来

 
阅读更多
import java.io.File;

/**
 * 运用IO流知识,编一个程序,将D盘目录下所有文件遍历出来
 * @author 够潮
 *
 */
public class Demo7 {
	
	
	/**
	 * 利用递归遍历
	 * @param filePath
	 */
	public static void getAllFile(String filePath){
		/**
		 * 绑定目录
		 */
		File dir = new File(filePath);
		/**
		 * 得到子目录和文件列表
		 */
		String fileList[] = dir.list();
		/**
		 * 遍历
		 */
		if(fileList == null)
			return ;
		for( int i= 0 ; i < fileList.length; i ++){
			String name = fileList[i];
			String pathName = filePath+File.separator+name;//得到当前目录的全路径
			File curFile = new File(pathName);
			/**
			 * 如果当前是目录
			 */
			if(curFile.isDirectory() ){
				System.out.println(pathName+":");
				/**
				 * 递归
				 */
				getAllFile(pathName);
				
			}
			/**
			 * 如果当前是文件
			 */
			if(curFile.isFile()){
				System.out.println(pathName) ;
				
			}
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Demo7.getAllFile("D:\\");

	}
	
	
	

}

 

0
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics