模糊查询文件是否存在
来源:互联网 发布:图片热点 js 编辑:程序博客网 时间:2024/06/17 07:05
项目中用到模糊查询获取文件,因为有些文件以指定头尾和时间节点来命名,在不知道是否有该文件或不知道文件全名的时候就用到了模糊查询来判断获取文件。
根据文件名称包含指定头尾字符判断是否存在对应文件
/***查找文件名称结尾为.txt的文件是否存在*filepath 文件路径*filename 文件名包含指定字符如:log_**/public String getFileName(String filepath, String filename){ File []fileArray = getFileList(filepath, filename); if (fileArray == null) return null; for (int i = 0; i < fileArray.length; i++) { if (fileArray[i] != null) && fileArray[i].getName().endsWith(".txt")){ System.out.println("文件存在"); return fileArray[i].getName(); } } System.out.println("文件不存在"); return null;}
/***获取filepath路径下包含str字符的文件, ***/public static File [] getFileList(String filePath, String str){ File file = new File(filePath); File [] fileArrays = new File[2]; int j = 0; //获取该目录下所有文件和目录的绝对路径 File [] fileArray = file.listFiles(new FileFilterUtils(str)); if (fileArray != null){ for (int i = 0; i < fileArray.length; i++) { if (fileArray[i] != null && fileArray[i].getName().contains(str)){ fileArrays[j] = fileArray[i]; j++; if (j == 2) break; } } } return fileArrays;}
文件过滤器,文件名filename开头返回true
public class FileFilterUtils implements FileFilter { private String filename = ""; public FileFilterUtils(String fileName){ this.filename = fileName; } @Override public boolean accept(File pathname) { if (pathname.isDirectory()){ return true; }else { String name = pathname.getName(); if (name.startsWith(filename)){ return true; }else { return false; } } }}
阅读全文
0 0
- 模糊查询文件是否存在
- JAVA查询文件是否存在,判断
- 查询目录或文件是否存在
- windows查询文件中是否存在字符串
- 用idftp控件查询ftp服务器文件目录是否存在
- shell 查询当前的文件夹下是否存在某个文件
- 查询文件是否存在并改名(Shell脚本)
- VB学习8_VBA如何模糊判断包含某关键字的文件是否存在?
- 文件模糊查询
- 判断文件是否存在
- 判断文件是否存在
- 判断文件是否存在
- C++ 文件是否存在
- 判断文件是否存在
- 检测文件是否存在
- 检查文件是否存在
- 判断文件是否存在
- 判断文件是否存在
- java中关键字的定义以及如何识别关键字
- 使用MoveWindow()函数来移动指定句柄的窗口在屏幕中的显示位置及这个窗口的大小
- 理解ARM中断原理以及中断嵌套
- 五星评价(包含半个星星)
- 12. Integer to Roman
- 模糊查询文件是否存在
- Leetcode563.+Leetcode543. DFS之应用(一):求解树计算问题
- 冒泡排序——C#实现
- supervisor和nohup
- Android 设计模式—单例模式
- Unity3d ulua c#与lua交互+wrap文件理解
- 顺序表应用3:元素位置互换之移位算法
- Shiro(3)——授权
- 何时使用img标签,何时使用background-image背景图像