java实现打印某个目录的全部子文件名称

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

[java]

import java.io.*; //实现打印某个目录的全部子文件名称(包括子目录)

import java.util.Scanner;

public class Test { //命令行参数需要提供一个目录参数

public static void main(String[] args) throws Exception {

if (args.length!=1) {

System.out.println(”Usage: java FileExample directory”);

System.exit(0);

}

new Test()。printFile(new File(args[0]));

Scanner in = new Scanner(System.in);

String dir = “”, filename = “”;

System.out.println(”输入目录名:”);

dir = in.next();

System.out.println(”输入文件名:”);

filename = in.next();

Test t = new Test();

File f = new File(dir);

int i = 0;

if( (i = t.find(f, filename)) == 0 ){

System.out.println(”该目录下没有此文件。”);

}else{

System.out.println(”共找到” + i + “个文件。”);

}

System.out.println(dir + ” 目录下,文件总大小:” + t.totalSize(dir));

}

public void printFile(File f) throws Exception {

if (!f.isDirectory()) {

System.out.println(”FileExample only accept directory parameter.”);

System.exit(0);

}

System.out.println(f.getCanonicalPath()); //输出规范的相对对路径

File[] children = f.listFiles(); //得到目录的子文件(包括子目录)

for(int i=0;i<children.length;i++) {

if (children[i].isDirectory()) { //如果是子目录,则递归

this.printFile(children[i]);

}

else {

System.out.println(children[i].getCanonicalPath());

}

}

}

[1][2]

接受失败更是一种智者的宣言和呐喊

java实现打印某个目录的全部子文件名称

相关文章:

你感兴趣的文章:

标签云: