package util;

import java.io.File;
import java.io.IOException; //列出File的一些常用操作
public class util {
/*
* 列出指定目录下(包括其子目录)的所有文件
*/
public static void listDirectory(File dir)throws IOException {
if(!dir.exists())
throw new IllegalArgumentException("目录:"+dir+"不存在.");
if(!dir.isDirectory()){
throw new IllegalArgumentException(dir+"不是目录。");
}
/*String[] filenames=dir.list();//返回的是字符串数组 直接子的名称 不包含子目录
for(String string:filenames){
System.out.println(dir+"\\"+string);
}*/
//如果要遍历子目录下的内容就需要构造File对象做递归操作,File提供了直接返回File对象的API
File[] files=dir.listFiles();
//for(File file:files){
//System.out.println(file);
if(files!=null&&files.length>0){
for(File file:files){
if(file.isDirectory())
//递归
listDirectory(file);
else
System.out.println(file);
}
}
}
}
package util;

import java.io.File;
import java.io.IOException; public class test { public static void main(String[] args)throws IOException {
util.listDirectory(new File("D:\\一些工具"));
} }

java_遍历文件目录的更多相关文章

  1. python3.4下遍历文件目录,不需要再特殊处理中文编码

    python3.4下遍历文件目录,不需要再特殊处理中文编码 直接使用os.walk来遍历中文目录. os.walk方法返回的是一个三元 tupple(dirpath, dirnames, filena ...

  2. 【Lua】Lua + openresty遍历文件目录

    OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项. 今天用OpenRest ...

  3. php实现遍历文件目录

    php实现遍历文件目录 一.总结 1.熟悉简单:很经典的例子,多看,然后发现熟悉了很简单 二.php实现遍历目录 php实现遍历目录 代码一: //遍历目录 function iteral($path ...

  4. go递归遍历文件目录

    package main import ( "fmt" "io/ioutil" "log" ) //文件目录树形结构节点 type dirT ...

  5. Linux 循环遍历文件目录

    操作系统: Unbuntu 问题域:在一个文件目录下,嵌套有多个子目录,需要遍历这些子目录,并在子目录下进行相关操作,譬如:批量重命名,目录下的文件:又或者需要,设定工程目录(mvn versions ...

  6. PHP文件操作:遍历文件目录

    <?php /*遍历目录,列出目录中的文件 * array scandir(string $directory [,int $sorting_order]) * $directory为待遍历目录 ...

  7. 使用php glob函数查找文件,遍历文件目录(转)

    函数说明:array glob ( string $pattern [, int $flags ] )功能:寻找与模式匹配的文件路径,返回包含匹配文件(目录)的数组(注:被检查的文件必须是服务器系统的 ...

  8. Java遍历文件目录

    函数介绍 File[] listFiles():返回当前文件的子目录或子文件的文件数组. 遍历目录 调用listFiles()即可得文件的子目录和子文件,如果存在子目录,那么子目录需要再次调用list ...

  9. python使用装饰器对文件进行读写操作'及遍历文件目录

    '''使用装饰器对文件进行读写操作''' # def check_permission(func): # '''演示嵌套函数定义及使用''' # def wrapper(*args,**kwargs) ...

随机推荐

  1. Ubuntu下非常规方法安装绿色软件(压缩包)

    继上一篇http://www.cnblogs.com/EasonJim/p/7117567.html文章中说的常规方式安装的软件,都会自动在命令行及Dash Home中体现. 但是如果是使用压缩包进行 ...

  2. jq仿苹果的时间/日期选择效果

    1.html文件,index.html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  3. CentOS 7: Install vsftpd

    Install vsftpd All commands should be run with ‘root’ user. Run the following command in terminal to ...

  4. [JavaEE] Create API documents with Swagger

    We mainly need to modify our Model and REST endpoint code to enable swagger Document. model/Book.jav ...

  5. sql 分组取每组的前n条或每组的n%(百分之n)的数据

    sql 分组取每组的前n条或每组的n%(百分之n)的数据 sql keyword: SELECT * ,ROW_NUMBER() OVER(partition by b.UserID order by ...

  6. leetcode_Product of Array Except Self

    描写叙述: Given an array of n integers where n > 1, nums, return an array output such that output[i]  ...

  7. 联想S820 MIUI刷机包 MIUI 4.4.30 流畅执行 在线主题破解

    ROM介绍 破解免费使用MIUI全部主题(方法:开机开启Root权限,进入WSM工具箱→安装二进制文件→重新启动→再次进入WSM工具箱→两个工具打上勾→重新启动),然后尽情奔放吧 .加入V4A音效 . ...

  8. hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...

  9. UVa563_Crimewave(网络流/最大流)(小白书图论专题)

    解题报告 思路: 要求抢劫银行的伙伴(想了N多名词来形容,强盗,贼匪,小偷,sad.都认为不合适)不在同一个路口相碰面,能够把点拆成两个点,一个入点.一个出点. 再设计源点s连向银行位置.再矩阵外围套 ...

  10. Struts2之类型转换器的使用

    一.学习案例:通过在输入页面(input.jsp)用同一个输入框同一时候输入username和password,通过类型转换器在输出页面(output.jsp)分别输出username和passwor ...