java 搜索文件

来源:互联网 发布:彩票平台源码 编辑:程序博客网 时间:2024/04/24 20:32
package com.zhang.test;


import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Test {
private Long num = 1L;
private String imsg = "";
/**多少个文件**/
private Map<String,Long> fileMaps = new HashMap<String,Long>();
/**多少个文件名字**/
private List<Object> fileName = new ArrayList<Object>();
/**多少个绝对路径文件**/
private List<Object> fileAbsolutePath = new ArrayList<Object>();
/**多少个文件路径**/
private List<Object> filePath = new ArrayList<Object>();
public Map<String, Long> getFileMaps() {
return fileMaps;
}
public List<Object> getFileName() {
return fileName;
}
public List<Object> getFileAbsolutePath() {
return fileAbsolutePath;
}
public List<Object> getFilePath() {
return filePath;
}
/**
* 找文件夹
* @param file
* @param str_name
* @return
*/
public synchronized void findByFiles(File file, String str_name){
File[] files = file.listFiles();
if(files!=null && files.length > 0){//文件夹
for (File file2 : files) {
// System.out.println("收索当前路径>>>"+file2.getPath());
if(file2.isDirectory()){//文件夹
imsg = findByFile(file2, str_name);
if(!imsg.equals("")){
fileMaps.put("fileNumber", num++);
fileName.add(file2.getName());
fileAbsolutePath.add(file2.getAbsolutePath());
filePath.add(file2.getPath());
}

findByFiles(file2, str_name);
}else{//不是文件夹
imsg = findByFile(file2, str_name);
if(!imsg.equals("")){
fileMaps.put("fileNumber", num++);
fileName.add(file2.getName());
fileAbsolutePath.add(file2.getAbsolutePath());
filePath.add(file2.getPath());
}
}
}
}else{//文件
// System.out.println("收索当前路径>>>"+file.getPath());
imsg = findByFile(file, str_name);
if(!imsg.equals("")){
fileMaps.put("fileNumber", num++);
fileName.add(file.getName());
fileAbsolutePath.add(file.getAbsolutePath());
filePath.add(file.getPath());
}
}
}
/**
* 找文件
* @param file
* @param str_name
* @return
*/
public String findByFile(File file, String str_name){
String imsg = "";
if(file.getName().contains(str_name)){
imsg = file.getPath();
}
return imsg;
}



public static void main(String[] args) throws ParseException {
File file = new File("E:\\");
String str_name = "Moban.dwt";
Test entity = new Test();
entity.findByFiles(file, str_name);
System.out.println(entity.getFileMaps().get("fileNumber"));
for (Object string : entity.getFilePath()) {
System.out.println(string);
}
// File[] roots = File.listRoots();
// for (int i =0; i < roots.length; i++) {
// System.out.println("收索 "+roots[i]+" 盘...");
// entity.getFileName(roots[i], str_name);
// System.out.println(roots[i]+" 盘结束...");
// }
}

}
0 0