易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Java读取某目录下所有文件名
Java读取某目录下所有文件名:
import
java.io.*;
import
java.util.*;
import
org.apache.log4j.Logger;
/**
* 读取目录及子目录下指定文件名的路径, 返回一个List
*/
public
class
FileViewer {
private
static
Logger logger = Logger.getLogger(FileViewer.
class
);
/**
* @param path
* 文件路径
* @param suffix
* 后缀名, 为空则表示所有文件
* @param isdepth
* 是否遍历子目录
* @return list
*/
public
static
List<String> getListFiles(String path, String suffix,
boolean
isdepth) {
List<String> lstFileNames =
new
ArrayList<String>();
File file =
new
File(path);
return
FileViewer.listFile(lstFileNames, file, suffix, isdepth);
}
private
static
List<String> listFile(List<String> lstFileNames, File f, String suffix,
boolean
isdepth) {
// 若是目录, 采用递归的方法遍历子目录
if
(f.isDirectory()) {
File[] t = f.listFiles();
for
(
int
i =
0
; i < t.length; i++) {
if
(isdepth || t[i].isFile()) {
listFile(lstFileNames, t[i], suffix, isdepth);
}
}
}
else
{
String filePath = f.getAbsolutePath();
if
(!suffix.equals(
""
)) {
int
begIndex = filePath.lastIndexOf(
"."
);
// 最后一个.(即后缀名前面的.)的索引
String tempsuffix =
""
;
if
(begIndex != -
1
) {
tempsuffix = filePath.substring(begIndex +
1
, filePath.length());
if
(tempsuffix.equals(suffix)) {
lstFileNames.add(filePath);
}
}
}
else
{
lstFileNames.add(filePath);
}
}
return
lstFileNames;
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图