文件查询器出现的问题~~请帮忙~~

baeeq0122 2008-03-13 09:36:46
代码如下:

package 文件查询器;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;



class CollectionFileName{

private String serchPath = null;// 搜索路径

String fileAccord = null;//搜索的文件名字

private int count = 0;// 计数器

private ArrayList<String> fileConvene = null;// 搜索后符合条件的文件集

// private String[] tempString = null;// 接收list()返回的数组

File path;// = new File(serchPath,getFileAccord());//最终路径


public CollectionFileName(){
serchPath = "ALL ABOUT JAVA";
File file = new File(File.separator);
path = new File(file,serchPath);

}

public CollectionFileName(String serchPath,String fileAccord){
this.serchPath = serchPath;
setFileAccord(fileAccord);


}

//文件累加器
public void count(){
count++;
}


//将符合条件的文件名字符串保存到ArrayList对象fileConvene中
public void filterFile(){

/* serchPath = "ALL ABOUT JAVA";
File file = new File(File.separator);
path = new File(file,serchPath);
*/
(如果取消上面三行的注释,能编译通过~添加注释后,抛出空指针异常~~)
(代码要求~~在Demo类中设置搜索的条件~查询并列出所需文件名~~)
String[] tempString;

tempString = getPath().list(new FilenameFilter() {
public boolean accept(File file, String name) {
return name.indexOf(fileAccord) != -1;
}
});//filter);

for(int i = 0; i < tempString.length; i++){
fileConvene.add(tempString[i]);
count();
}
}

public void printResult(){
Object[] o = fileConvene.toArray();
for(int i = 0; i < o.length; i++){
System.out.println(o[i]);
}
}

//搜索文件名设置
public void setFileAccord(String fileAccord){
this.fileAccord = fileAccord;
}
//返回搜索的文件名
public String getFileAccord(){
return fileAccord;
}

//get() and set()method
public ArrayList getFileConvene() {
return fileConvene;
}

//返回搜索全路径
public File getPath() {
path = new File(getSerchPath(),getFileAccord());
return path;
}
//设置搜索全路径
public void setPath(File path) {
this.path = path;
}
//返回parent搜索路径
public String getSerchPath() {
return serchPath;
}
// 设置parent搜索路径
public void setSerchPath(String serchPath) {
this.serchPath = serchPath;
}

//返回符合文件的个数
public int getCount() {
return count;
}

//过滤条件
public void setFileConvene(ArrayList<String> al){
fileConvene = al;
}

};

package 文件查询器;

import java.util.ArrayList;

public class Demo {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList<String> al = new ArrayList<String>();

// FilenameFilterDemo ffd = new FilenameFilterDemo();

CollectionFileName cfn = new CollectionFileName("H:\\","All ABOUT JAVA");

// CollectionFileName.FilenameFilterDemo ffd = cfn.new FilenameFilterDemo();

// cfn.setFilter(ffd);

cfn.setFileAccord("a");//搜索条件设置
cfn.setFileConvene(al);
cfn.filterFile();

cfn.printResult();
System.out.println();
System.out.println("符合文件的总数为: " + cfn.getCount() + "个");


}

}



...全文
97 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
olivesoup 2008-03-13
  • 打赏
  • 举报
回复
基本上还是你那套,就改了个路经而已


public class CollectionFileName {
private String serchPath = null;// 搜索路径

String fileAccord = null;// 搜索的文件名字

private int count = 0;// 计数器

private ArrayList<String> fileConvene = null;// 搜索后符合条件的文件集

// private String[] tempString = null;// 接收list()返回的数组

File path;// = new File(serchPath,getFileAccord());//最终路径

public CollectionFileName() {
serchPath = "ALL ABOUT JAVA";
File file = new File(File.separator);
path = new File(file, serchPath);

}

public CollectionFileName(String serchPath, String fileAccord) {
this.serchPath = serchPath;
setFileAccord(fileAccord);

}

// 文件累加器
public void count() {
count++;
}

// 将符合条件的文件名字符串保存到ArrayList对象fileConvene中
public void filterFile() {

serchPath = "D:\\"; //★★★ 1.这里用我本地的盘符
// serchPath = "ALL ABOUT JAVA";
File file = new File(File.separator);
path = new File(file, serchPath);

// (如果取消上面三行的注释,能编译通过~添加注释后,抛出空指针异常~~)
// (代码要求~~在Demo类中设置搜索的条件~查询并列出所需文件名~~)
String[] tempString;
tempString = getPath().list();//★★★ 2.这里暂时不用过滤器
// tempString = getPath().list(new FilenameFilter() {
// public boolean accept(File file, String name) {
// return name.indexOf(fileAccord) != -1;
// }
// });// filter);

for (int i = 0; i < tempString.length; i++) {
fileConvene.add(tempString[i]);
count();
}
}

public void printResult() {
Object[] o = fileConvene.toArray();
for (int i = 0; i < o.length; i++) {
System.out.println(o[i]);
}
}

// 搜索文件名设置
public void setFileAccord(String fileAccord) {
this.fileAccord = fileAccord;
}

// 返回搜索的文件名
public String getFileAccord() {
return fileAccord;
}

// get() and set()method
public ArrayList getFileConvene() {
return fileConvene;
}

// 返回搜索全路径
public File getPath() {
path = new File(getSerchPath(), getFileAccord());
return path;
}

// 设置搜索全路径
public void setPath(File path) {
this.path = path;
}

// 返回parent搜索路径
public String getSerchPath() {
return serchPath;
}

// 设置parent搜索路径
public void setSerchPath(String serchPath) {
this.serchPath = serchPath;
}

// 返回符合文件的个数
public int getCount() {
return count;
}

//过滤条件
public void setFileConvene(ArrayList<String> al) {
fileConvene = al;
}

}





public class Demo {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList <String> al = new ArrayList <String> ();

// FilenameFilterDemo ffd = new FilenameFilterDemo();

CollectionFileName cfn = new CollectionFileName("D:\\","All ABOUT JAVA");

// CollectionFileName.FilenameFilterDemo ffd = cfn.new FilenameFilterDemo();

// cfn.setFilter(ffd);

cfn.setFileAccord("temp");//★★★ 3.这里指定我本地D盘下的temp文件夹
cfn.setFileConvene(al);
cfn.filterFile();

cfn.printResult();
System.out.println();
System.out.println("符合文件的总数为: " + cfn.getCount() + "个");


}
}

baeeq0122 2008-03-13
  • 打赏
  • 举报
回复
.....................
我有点晕了~
呵呵~~改的地方太多了~~
麻烦你把你修改后的代码贴出来吧 ~~
谢谢~~
olivesoup 2008-03-13
  • 打赏
  • 举报
回复
在你的Demo类中,最后设定查寻文件名为a,并且cfn.filterFile();
查看了一下filterFile()方法,一上来就重设了serchPath,serchPath = "ALL ABOUT JAVA";
也就是说你想在ALL ABOUT JAVA目录下寻找,可是这样的话就将H:\\这个主目录丢失了,
注意CollectionFileName cfn = new CollectionFileName("H:\\","All ABOUT JAVA");
的时候本已经设好了主目录为H:\\,这样在"ALL ABOUT JAVA"目录下自然找不到任何文件,
你应该将主目录加在前面:"H:\\All ABOUT JAVA\\"

我试了一下,其他地方没什么大问题,结果如下
Data.Cab
Data1.cab
instmsia.exe
instmsiw.exe
Readme.doc
Readme.pdf
setup.exe
setup.ini
setup.msi

符合文件的?数?: 9个
olivesoup 2008-03-13
  • 打赏
  • 举报
回复
第一个问题:
你的过滤器是为了限制文件检索条件,

cfn.setFileAccord("a");//搜索条件设置

我为了快速检查你程序的问题,将这个限制条件去掉了,
没什么特别原因,去掉这部分对程序其他地方没什么影响,
只是检索出来的范围放大了而已,让我集中精力,不必关系这块

第二个问题:
提供一个思路,你的tempString = getPath().list();这个地方将结果以字符串的形式取出来,
但取出来的是文件夹还是文件靠字符串是不能判断的,你可以用下面提供的方法进行修改

File[] files = new File("D:\\temp\\").listFiles();//以File数组的形式返回
for (File file : files) {
if (file.isFile()) {
// do file...
} else if (file.isDirectory()) {//如果是文件夹,需要将这个file重新进行file.listFiles();遍历,重复for循环这个过程
// do directory....
}
}



baeeq0122 2008-03-13
  • 打赏
  • 举报
回复
十分感谢~~
有几个问题想再请教一下~~
问题一:
tempString = getPath().list();//★★★ 2.这里暂时不用过滤器 (为什么可以暂时不用过滤器~)
问题二:
搜索过程中~如果碰到文件夹中的文件夹,这种情况如何过滤得到文件夹中的文件夹名然后装入另一个ArrayList中~进行再次查询~~

62,623

社区成员

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

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