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]+" 盘结束...");
// }
}
}
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
- JAVA实现文件搜索
- java文件受限搜索
- java 搜索文件
- java 搜索文件
- java搜索类文件次序
- java.IO搜索指定文件
- java CountDownLatch 多线程文件搜索
- 文件搜索器_FileSearch.java
- java .so文件搜索路径
- JAVA文件学习,文件搜索器
- Java用广度优先搜索快速搜索文件
- 仿Windows文件搜索 JAVA 实现
- java 搜索指定后缀名的所有文件
- java 大文件数据搜索技术
- 使用Java搜索特定后缀名的文件
- java编写文件搜索器心得
- java使用部分字符串搜索文件
- Java基础-实现文件搜索功能
- JFreeChart 使用 一 直方图之柱状图-高级特性
- 为什么Android与PC平台按照opengl坐标贴纹理会出现颠倒
- 第九周项目4
- 外文文献查询网站
- 《编写高质量代码:改善Java程序的151个建议》 建议9
- java 搜索文件
- Running the app on your device
- Mark走起!iOS开发最常用的第三方库
- JFreeChart 高级应用 一 定制图表
- C语言 已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。
- Java Dom Parse XML
- 外文文献查询网站
- dgsgds噶萨嘎萨嘎水开后更快更好看看过后
- 定位new 运算符