读取并列出指定目录下文件

读取并列出指定目录下文件,实现类似linux下ls,dos下dir的功能:

调取dir即可

public void dir(String directory){
walkdir(new File(directory), 0);
}

private void walkdir(File f, int level){
if (f.isDirectory()) {
for (int i = 0; i < level; i++) {
System.out.print(“…”);
}
System.out.println(f.getName());
for (File inner : f.listFiles()) {
walkdir(inner, level + 1);
}
}else {
for (int i = 0; i < level; i++) {
System.out.print(“…”);
}
System.out.println(f.getName());
}
}

This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s