求搜索目录算法

dgsrest 2003-10-16 12:29:49
我想写个程序,用户自定义路径,然后把它的路径下的文件全部找出来。
...全文
32 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
207 2003-10-16
  • 打赏
  • 举报
回复
这个用循环的
//:用循环方法实现
package ces.com.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 BufferedReader(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("输入信息不正确,转入搜索默认目录");
strDirRoot="c:\\downloads";
}

//提示
System.out.println("正在搜索,请稍候...");

//调用getFileTreeToVector方法
try{
getFileTreeToVector(strDirRoot);

//调整向量内的确切容量
vcFileTree.trimToSize();

//打印向量内的内容
for(int i=0;i<vcFileTree.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){

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;i<fileSubRoot.length;i++)

//如果不是文件夹
if(!fileSubRoot[i].isDirectory()){

//把文件路径信息放入文件树Vector
vcFileTree.add(fileSubRoot[i]);
}
else{
vcFileTree.add(fileSubRoot[i]);
//是文件夹,放入文件夹Vector内
vcDir.add(fileSubRoot[i]);
}
}

//如果文件夹向量读完,就退出
if(intDir==vcDir.size()) break;

try{
/**
* 从文件夹向量读出子文件夹信息
* 改变类成员fileTmpDir内容记录准备搜索的文件夹信息
*/
fileTmpDir=(File)vcDir.get(intDir);
}catch(Exception e){
e.printStackTrace();
}

//类成员记住读取文件夹向量中元素位置
intDir++;

} //for(;;)结束
return vcFileTree;
}
}
207 2003-10-16
  • 打赏
  • 举报
回复
我写的搜索的,这个是递归的
//:用递归方法实现

package ces.com.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 BufferedReader(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("正在搜索,请稍候...");


//调用getFileTreeToVector方法
try{
getFileTreeToVector(strDirRoot);

//调整向量内的确切容量
vcFileTree.trimToSize();

//打印向量内的内容
for(int i=0;i<vcFileTree.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();

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;i<fileSubRoot.length;i++)

//如果不是文件夹
if(!fileSubRoot[i].isDirectory()){

//把文件路径信息放入Vector
vcFileTree.add(fileSubRoot[i]);
}

//如果是文件夹,递归调用
else{
//把文件夹路径信息放入Vecot
vcFileTree.add(fileSubRoot[i]);

//在以该文件夹路径为相对根目录再递归搜索
File[] fileSubPaths =fileSubRoot[i].listFiles();

//递归调用
loopSearch(fileSubPaths);
}
}
else{}
}// loopSearch方法结束

}//DirMap类结束


62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧