1
計數文件到目前爲止,我有這樣的代碼:清單,並通過它們的擴展
import java.io.File;
import java.util.Scanner;
public class Test {
static Scanner input = new Scanner(System.in);
public static void fileListing(File[] files, int depth) {
if(depth == 0)
return;
else {
for(File file: files) {
if(file.isDirectory())
fileListing(file.listFiles(), depth-1);
else {
String ext;
String fileName = file.getName();
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
ext = fileName.substring(fileName.lastIndexOf(".")+1);
else
return;
System.out.println(ext);
}
}
}
}
public static void main(String [] args) {
System.out.printf("Path: ");
String path = input.nextLine();
if(new File(path).isDirectory()) {
System.out.printf("Depth: ");
int depth = input.nextInt();
File[] file = new File(path).listFiles();
fileListing(file, depth);
}
else {
System.out.printf("The path %s isn't valid.", path);
System.exit(0);
}
}
}
我的輸出列表中的文件中的某個目錄的擴展,E。 G。
txt
txt
doc
如何改進此代碼以顯示文件的擴展名與計數器?例如上面,輸出應該是這樣的:
2 txt
1 doc
現在看看我的代碼,我增加了一個擴展微調。你能告訴我如何爲我的代碼添加一個計數器嗎?或者如何將您的代碼與我的代碼合併? – JohnDoe 2014-11-24 19:27:39