
目录搜索算法.pdf
7页我想写个程序,用户自定义路径,然后把它的路径下的文件全部找出来我写的搜索的,这个是递归的/:用递归方法实现package .filesys;import java.io.*;import java.util.*;/*Title:目录列表*Description:据用户输入根目录列出子目录和其文件*/public class DirMap /存储根目录下文件树信息 private static Vector vcFileTree=new Vector();/存储用户输入的标准路径信息 private static String strDirRoot=new String();/主方法 public static void main(String args)throws Exception /暂时存储用户输入的路径 String strTmp=new String();try /接受用户输入搜索路径InputStreamReader reader=new InputStreamReader(System.in);BufferedReader input=new BufferedR eader(reader);System.out.println(请输入你要搜索的目录:);strTmp=input.readLine();/转换为标准路径信息strDirRoot=strTmp.substring(0,1)+:+strTmp.substring(2);catch(IOException ioe)System.err.println(ioe.toString();catch(StringIndexOutOfBoundsException se)/回车空信息判断,转入搜索默认目录 System.err.println(输入信息为空,转入搜索默认目录);/提示 System.out.println(正在搜索,请稍候.);名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -/调用 getFileTreeToVector 方法 try getFileTreeToVector(strDirRoot);/调整向量内的确切容量vcFileTree.trimToSize();/打印向量内的内容for(int i=0;ivcFileTree.size();i+)System.out.println(vcFileTree.get(i);System.out.println(搜索完毕!);catch(Exception e)System.err.println(e.toString();/*param strDirRoot 当前要搜索根目录名 *return 存储该根目录下文件树信息的向量 */public static Vector getFileTreeToVector(String strDirRoot)File fileRootPath=new File(strDirRoot);/*判断路径是否存在 *如果用户输入目录不存在,自动跳到C:downloads目录下 */if(!fileRootPath.exists()System.err.println(路径不存在,转入搜索默认路径!);/路径不存在,设置搜索默认路径strDirRoot=c:downloads;fileRootPath=new File(strDirRoot);else /读出相对根目录下文件树信息File fileSubPath =fileRootPath.listFiles();名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7 页 -try /开始搜索loopSearch(fileSubPath);catch(Exception e)System.err.println(e.toString();return vcFileTree;/getFileTreeToVector 方法结束 /*param fileSubRoot 待搜索相对目录下的文件树信息 */private static void loopSearch(File fileSubRoot)throws Exception /如果不是空文件夹if(fileSubRoot!=null)for(int i=0;ifileSubRoot.length;i+)/如果不是文件夹if(!fileSubRooti.isDirectory()/把文件路径信息放入Vector vcFileTree.add(fileSubR ooti);/如果是文件夹,递归调用 else /把文件夹路径信息放入Vecot vcFileTree.add(fileSubR ooti);/在以该文件夹路径为相对根目录再递归搜索File fileSubPaths =fileSubRooti.listFiles();/递归调用loopSearch(fileSubPaths);else/loopSearch 方法结束名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -/DirMap类结束这个用循环的/:用循环方法实现package .filesys;import java.io.*;import java.util.*;/*Title:目录列表*Description:据用户输入根目录列出子目录和其文件*/public class DirMap2 /存储根目录下文件树信息 private static Vector vcFileTree=new Vector();/存储文件夹信息 private static Vector vcDir=new Vector();/存储暂时相对根目录 private static File fileTmpDir;/存储用户输入的标准路径信息 private static String strDirRoot=new String();/记录文件夹向量里读取第几个元素 private static int intDir=0;/主方法 public static void main(String args)throws Exception /暂时存储用户输入的路径 String strTmp=new String();try /接受用户输入搜索路径InputStreamReader reader=new InputStreamReader(System.in);BufferedReader input=new BufferedR eader(reader);名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 7 页 -System.out.println(请输入你要搜索的目录:);strTmp=input.readLine();/转换为标准路径信息strDirRoot=strTmp.substring(0,1)+:+strTmp.substring(2);catch(IOException ioe)System.err.println(ioe.toString();catch(StringIndexOutOfBoundsException se)/信息判断,转入搜索默认目录 System.err.println(输入信息不正确,转入搜索默认目录);strDirRoot=c:downloads;/提示 System.out.println(正在搜索,请稍候.);/调用 getFileTreeToVector 方法 try getFileTreeToVector(strDirRoot);/调整向量内的确切容量vcFileTree.trimToSize();/打印向量内的内容for(int i=0;ivcFileTree.size();i+)System.out.println(vcFileTree.get(i);System.out.println(搜索完毕!);catch(Exception e)System.err.println(e.toString();/*用循环方法实现目录搜索 *param strDirRoot 当前要搜索根目录名 *return 存储该根目录下文件树信息的向量 */public static Vector getFileTreeToVector(String strDirRoot)名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 7 页 -fileTmpDir=new File(strDirRoot);/*判断路径是否存在 *如果用户输入目录不存在,自动跳到C:downloads目录下 */if(!fileTmpDir.exists()System.err.println(路径不存在,转入搜索默认路径!);/路径不存在,设置搜索默认路径strDirRoot=c:downloads;fileTmpDir=new File(strDirRoot);else /读出相对根目录下文件树信息 for(;)/*用类成员 fileTmpDir 记录文件夹信息,便于循环 *读取相对文件夹里文件树信息 */File fileSubRoot=fileTmpDir.listFiles();/如果不是空文件夹if(fileSubRoot!=null)for(int i=0;ifileSubRoot.length;i+)/如果不是文件夹if(!fileSubRooti.isDirectory()/把文件路径信息放入文件树Vector vcFileTree.add(fileSubR ooti);else vcFileTree.add(fileSubR ooti);/是文件夹,放入文件夹Vector 内vcDir.add(fileSubRooti);/如果文件夹向量读完,就退出if(intDir=vcDir.size()break;名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 7 页 -try /*从文件夹向量读出子文件夹信息 *改变类成员 fileTmpDir 内容记录准备搜索的文件夹信息 */fileTmpDir=(File)vcDir.get(intDir);catch(Exception e)e.printStackTrace();/类成员记住读取文件夹向量中元素位置intDir+;/for(;)结束return vcFileTree;名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 7 页 -。












