java的File机制实现目录下的文件检索
来源:互联网 发布:安卓黑科技软件网站 编辑:程序博客网 时间:2024/04/29 23:21
本文章纯属原创,未经声明不得转载
学习了java的File机制,通过命令行确定三个参数
isRecusively(是否递归读取二级目录文件) , isVerbose(是否显示文件大小),RootPath(文件根目录)来实现了一个文件检索器,用于打印出所有非目录文件
import java.io.File;import java.util.ArrayList;/** * DirListing * @Junyi Li * 功能:用于完成某目录下的文件检索 */public class DirListing { private ArrayList<File> fileList; private boolean isRecusively; private boolean isVerbose; private String rootPath; public static void main(String[] args) { boolean recursively = false; boolean use_Verbose = false; boolean ArgsValid = true; File f = new File(System.getProperty("user.dir")); String curPath = f.getPath(); //参数读取 switch (args.length) { case 0: break; case 1: if (args[0].equals("-R") || args[0].equals("-r")) { recursively = true; } else if (args[0].equals("-verbose") || args[0].equals("-VERBOSE")) { use_Verbose = true; } else if (args[0].equals("non-verbose") || args[0].equals("NON-VERBOSE")) { } else { curPath = args[0]; } break; case 2: if (args[0].equals("-R") || args[0].equals("-r")) { recursively = true; if (args[1].equals("-verbose") || args[1].equals("-VERBOSE")) { use_Verbose = true; } else if (args[1].equals("non-verbose") || args[1].equals("NON-VERBOSE")) { } else { curPath = args[1]; } } else { if (args[0].equals("-verbose") || args[0].equals("-VERBOSE")) { use_Verbose = true; } else if (args[0].equals("non-verbose") || args[0].equals("NON-VERBOSE")) { use_Verbose = false; } else { ArgsValid = false; System.out.println("Here is one or more argument are invalid!"); break; } curPath = args[1]; } break; case 3: if (args[0].equals("-r") || args[0].equals("-R")) { recursively = true; } else { ArgsValid = false; System.out.println("Here is one or more argument are invalid!"); break; } if (args[1].equals("-verbose") || args[1].equals("-VERBOSE")) { use_Verbose = true; } else if (args[1].equals("non-verbose") || args[1].equals("NON-VERBOSE")) { use_Verbose = false; } else { ArgsValid = false; System.out.println("Here is one or more argument are invalid!"); break; } curPath = args[2]; break; } if (!(new File(curPath).exists())) {//文件路径正确性判断 System.out.println("Wrong path!"); ArgsValid = false; } if (ArgsValid) {//参数正确性判断 DirListing dl = new DirListing(recursively, use_Verbose, curPath); dl.ListingPrint(use_Verbose); } else { System.out.println("Nothing to print!"); } } //结果打印 public void ListingPrint(boolean isVerbose) { if (isVerbose) { for (int i = 0; i < fileList.size(); i++) { File f = fileList.get(i); String s = f.getPath().replace(rootPath, ""); System.out.println(" " + (f.isFile() ? f.length() : "<DIR>") + " " + s); } System.out.println(" " + "size" + " " + "name"); } else { for (int i = 0; i < fileList.size(); i++) { File f = fileList.get(i); String s = f.getPath().replace(" " + rootPath, ""); System.out.println(s); } System.out.println(" " + "name"); } } /** * 构造函数 * 参数1:是否深入读到各子目录,缺省则否 * 参数2:是否显示文件大小,若为文件夹是否显示为目录属性,缺省则否 * 参数3:指定根目录,缺省则当前文件目录 */ public DirListing(boolean Recusively, boolean Verbose, String RootPath) { File fff = new File(""); try { isRecusively = Recusively; rootPath = RootPath; isVerbose = Verbose; String path = RootPath; fileList = new ArrayList<File>(); if (isRecusively) {//递归地查询所有目录下的根目录 if (new File(path).isFile()) { fileList.add(new File(path)); } else { File[] curList = new File(path).listFiles(); boolean over = false; ArrayList<File> curDir = new ArrayList<File>(); for (int i = 0; i < curList.length; i++) { if (curList[i].isFile()) { fileList.add(curList[i]); } else { curDir.add(curList[i]); } } while (!over) { ArrayList<File> tmpDir = new ArrayList<File>(); for (int i = 0; i < curDir.size(); i++) { curList = curDir.get(i).listFiles(); for (int j = 0; j < curList.length; j++) { fff = curList[j];//用于某些系统禁读文件的保护,特殊输出 if (curList[j].isFile()) { fileList.add(curList[j]); } else { tmpDir.add(curList[j]); } } } curDir = tmpDir; if (curDir.size() == 0) over = true;//若不再有目录型File对象,则递归结束。 } } } else {//若不递归深入,则将一级目录下的文件和文件夹存入列表 File[] curList = new File(path).listFiles(); for (int i = 0; i < curList.length; i++) { fileList.add(curList[i]); } } } catch (Exception e) { System.out.println(); System.out.println(fff.getPath().replace(rootPath, "") + "can not be read!"); System.out.println(); } }}
默认无参时:
确定大小和递归读取子目录:
读取其他文件夹:
功能实现完成!
总结:
1.这段代码主要用到了java.io包中的File类的相关函数,如isFile()判断是否为文件,length确定长度(字节数),listFiles在该File对象为目录的前提下用于返回目录下所有File对象的数组。
2.其中检索所有文件的思想基本无优化
1 0
- java的File机制实现目录下的文件检索
- java file删除目录下的文件及文件夹
- java file 删除目录下无用的文件
- java工具类-给定一个file实例将该目录下的文件以及目录输出
- 循环双链表加目录IO实现简单的文件检索
- java:递归实现输出目录下所有的文件(列出目录下所有的文件)
- Java精选笔记_IO流【File(文件)类、遍历目录下的文件、删除文件及目录】
- Java文件 File练习-递归显示某一目录下的所有文件
- 习题13:编写程序,使用File类实现删除当前目录下的所有文件。
- 用java实现获取某目录下的所有文件
- Java实现复制一个目录下的所有文件
- Java利用File类将制定目录下的所有文件搜索出来
- file类列出当前目录下的文件
- File(遍历一个目录下的所有文件)
- VC下利用多线程实现文件的快速检索
- VC下利用多线程实现文件的快速检索
- VC下利用多线程实现文件的快速检索
- java 将源目录的所有文件(含子目录)拷贝到 目标目录中(运用File类实现)
- 行为型模式(二)
- 第七周项目5 排队看病模拟
- fadora21 声卡驱动消失问题
- JS产生随机数的几个用法!
- iOS最全面试题及答案
- java的File机制实现目录下的文件检索
- 用Matplotlib绘制股票每月最高收盘价曲线
- 数组中有字典的排序
- hdu 1633 数位DP 好题
- android选择图片得到实际路径的总结
- proto-lua-gen 转换proto文件错误 Expected top-level statement <e.g ."message">.
- 用mount挂载远程Windows共享文件夹进行文件拷贝的方法
- Unity日志工具——封装,跳转
- 数据类型的范围