求解两道java题目,请发送源代码!
发布网友
发布时间:2023-08-07 23:35
我来回答
共1个回答
热心网友
时间:2023-08-09 16:49
第二题的
import java.io.File;
public class FileList {
public static void printContentInOneDirectory(File path, int depth) {
for(int i = 0; i < depth; i++)
System.out.print("--");
if(path.isDirectory())
System.out.print("[目录] ");
else
System.out.print("[文件] ");
System.out.println(path.getName());
}
public static void readFileAndDirectory(File path, int depth) {
if(path.exists()) {
printContentInOneDirectory(path, depth);
if(path.isDirectory()) {
File[] files = path.listFiles();
for(File file : files)
readFileAndDirectory(file, depth + 1);
}
}
}
public static void main(String[] args) {
FileList.readFileAndDirectory(new File("D:/GIF Movie Gear"), 0);
}
}