经典方法 Link to heading
找出 path 的长度, 获取 path[i] 为文件名. File 类的 list 方法.
class Main {
public static void main(String[] args) {
File dir = new File("C:");
String[] children = dir.list();
if (children == null) {
System.out.println( "目录不存在或它不是一个目录");
}
else {
for (int i=0; i< children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
输出结果
build
build.xml
destnfile
detnfile
filename
manifest.mf
nbproject
outfilename
src
srcfile
test
java8 新特性 Link to heading
java8新引入了 lamda. lamda 个人理解就是不用正式命名的函数(当然还有很多细节). 如果不太熟悉可以上网搜一下.
Files.list(Paths.get("."))
.forEach(System.out::println);
Files.walk Link to heading
try(Stream<Path> paths = Files.walk(Paths.get("xxx"))){
paths.forEach(filePath->{
});
}
catch(e){
}