怎样取得某目录下的文件名,支持通配符(?,*)

ghw 2001-05-31 04:06:00
怎么取得某目录下的文件名(不包括子目录),支持通配符(?,*),就像windows的search功能一样;而且取到文件名后怎么按文件名排序,怎么按最后修改时间排序?

请哪位贴段代码上来? (Java的File类不太好用,我试了试,没能实现上述功能)
...全文
509 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghw 2001-06-06
  • 打赏
  • 举报
回复
给分喽
ghw 2001-06-05
  • 打赏
  • 举报
回复
to bootcool:
你的方法我昨天试了试,确实有一些文件查不出;jdk1.4还是beta版呢,还是不用了。
非常感谢你,贴子再留一天,我明天就给分。

我是初学java(一个多月),以后还请你多多指教。
ghw 2001-06-03
  • 打赏
  • 举报
回复
非常感谢bootcool,明天到公司试试你的方法。
hello_wyq 2001-06-02
  • 打赏
  • 举报
回复
bootcool(bootcool) :
好热情噢!
ghw 2001-06-01
  • 打赏
  • 举报
回复
to binriyue:
不是,我是想在无界面的程序中使用上述功能,不过用java的File类来实现很麻烦(比如windows下的文件名是不区分大小写的,而java的排序功能是分大小写的;按最后修改时间排序要取每个文件的最后修改时间,然后再排序;支持通配符要自己实现FilenameFilter接口),我想知道java中有没有现成的类,有没有简单点的方法。
binriyue 2001-06-01
  • 打赏
  • 举报
回复
是的意思是想让用户在文件选择框里输入几个字符,该选择框就只列出包含输入字符串的文件?
pcyang 2001-06-01
  • 打赏
  • 举报
回复
上 http://forum.java.sun.com 上看看,有范例
v不吃v你 2001-06-01
  • 打赏
  • 举报
回复
//如果你用的是jdk1.4,可以这样做。

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter; //not java.io.FileFilter
import java.io.File;
import java.util.*;
import java.util.regex.*;//case insensitivepublic
class RegexFilter extends FileFilter{
public RegexFilter(String description, String regex) {
m_description = description;
m_pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
}
public boolean accept(File f) {if (f.isDirectory()) return true;
String name = f.getName();
System.out.println("comparing " + name);
Matcher matcher = m_pattern.matcher(name);
return matcher.matches();
}
public String getDescription(){return m_description;}
private Pattern m_pattern;
private String m_description;
public static class Test{
public static void main(String[] argv){
JFileChooser chooser = new JFileChooser(new File("."));
chooser.setDialogTitle("Choose an image file");
chooser.setMultiSelectionEnabled(false);
RegexFilter filter = new RegexFilter("*java", ".*\\.java");
chooser.addChoosableFileFilter(filter);
chooser.showOpenDialog(null);
System.out.println(chooser.getSelectedFile());
}
}
}
v不吃v你 2001-06-01
  • 打赏
  • 举报
回复
//ghw老兄,用这个试试。
//但是还是有一些形式的文件名搜不出。
//文件名是这样的:
//gigi1.jpg,gigi2.jpg,gigi3.jpg,bgigi.jpg,tgirl.jpg....
//

import java.io.*;

public class FindWildcardFiles {
public FindWildcardFiles(){
File f = new File( "C:/My Documents/My Pictures" );
theFilter filter= new theFilter("*gi*i*.jpg");//也可为,*gi*但是对gi*的形式就不行了,再想想........
File[] files = f.listFiles(filter);

for ( int i=0; i<files.length-1; i++ ){
System.out.println(files[i]);
}
}
public static void main( String[] args ){
new FindWildcardFiles();
}
}

class theFilter implements FileFilter{

private char[] pattern;

public theFilter(String s){
s = s.trim();
s = toMinimalPattern(s);
s = s.toUpperCase();
pattern = s.toCharArray();
}

public boolean accept(File pathname){
String s = pathname.getPath();
s = s.toUpperCase();
char[] ca = s.toCharArray();
return match(pattern,0,ca,0);
}

public static String toMinimalPattern(String s){
int len = s.length();
StringBuffer sb = new StringBuffer();
int x = 0;
int y = s.indexOf('*');
while (x <= y){
y++;
sb.append(s.substring(x,y));
x = y;
while (y < len && s.charAt(y) == '*'){
y++;
x = y;
}
y = s.indexOf('*',x);
}
sb.append(s.substring(x));
return sb.toString();
}

public static boolean match(String pattern,String fileName){
pattern = pattern.trim();
pattern = toMinimalPattern(pattern);
pattern = pattern.toUpperCase();
char[] ca1 = pattern.toCharArray();
fileName = fileName.trim();
fileName = fileName.toUpperCase();
char[] ca2 = fileName.toCharArray();
return match(ca1,0,ca2,0);
}

private static boolean match(char[] pattern,int px, char[] name, int nx){
while (px < pattern.length){
char pc = pattern[px];
if (pc == '*'){
if (px >= (pattern.length - 1))
return true;
int x = nx;
while (x < name.length){
if (match(pattern,px+1,name,x))
return true;
x++;
}
px++;
continue;
}
if (nx >= name.length)
return false;
if (pc != '?'){
if (pc != name[nx])
return false;
}
px++;
nx++;
}
return (nx >= name.length);
}

public static boolean isMatch(String mask, String nick) {
mask = mask.trim();
if (mask.equalsIgnoreCase("*")) {
return true;
}
if (mask.startsWith("*")) {
int end = -1;
if (mask.indexOf("*",mask.indexOf("*") + 1) == -1) {
end = mask.length();
} else {
end = mask.indexOf("*",mask.indexOf("*") + 1);
}
if (end == -1) {
return false;
}
String tmask = mask.substring(1, end).trim();
if (nick.indexOf(tmask) == -1) {
return false;
} else {
mask = mask.substring(end).trim();
nick = nick.substring(nick.indexOf(tmask)).trim();
return isMatch(mask, nick);
}
}
int end = -1;
if (mask.indexOf("*") == -1) {
end = mask.length();
} else {
end = mask.indexOf("*");
}
if (end == -1) {
return false;
}
String tmask = mask.substring(0, end).trim();
if (nick.startsWith(tmask)) {
if (end == mask.length()) {
return true;
}
mask = mask.substring(end).trim();
nick = nick.substring(tmask.length()).trim();
return isMatch(mask, nick);
} else {
return false;
}
}
}

v不吃v你 2001-06-01
  • 打赏
  • 举报
回复
//这是找文件的。
//不过是找后缀名。
//我也在想如何加入通配符查找
//不过我想MYFileFilter上面的方法还是有用的。

import java.io.*;

class SearchFile{

public static void main(String args[]){
File aDirectory = new File("C:/My Documents/My Pictures");
String extension =".jpg";
find(aDirectory,extension,true);
}

static void find(File currentDirectory,String extension,boolean
includeSubdirectories){

if(!currentDirectory.isDirectory()){
System.out.println (currentDirectory.toString()+ " doesnot exists " );
return;
}

File[] currentDirectoryFileNames = currentDirectory.listFiles();
for(int i=0;i<currentDirectoryFileNames.length;i++){
if(currentDirectoryFileNames[i].isFile()){
String s= currentDirectoryFileNames[i].getName();
int index = s.indexOf('.');
if(!(index <0)){
String extensionofThisFile = s.substring(index);
if(extensionofThisFile.equals(extension)){
System.out.println(currentDirectoryFileNames[i]);
}
}
}
}

if(includeSubdirectories){
for(int i=0;i<currentDirectoryFileNames.length;i++){
if(currentDirectoryFileNames[i].isDirectory()){
find (currentDirectoryFileNames[i],extension,includeSubdirectories);
}
}
}
}
}
ghw 2001-06-01
  • 打赏
  • 举报
回复
to bootcool:
你的MYFileFilter类不好用,我试了试,连"c:\\"这么简单的目录名下的文件名都读不出来。
ghw 2001-06-01
  • 打赏
  • 举报
回复
为什么我问的问题总是没人回答? 大家交流交流经验嘛。
v不吃v你 2001-06-01
  • 打赏
  • 举报
回复
//看看这段程序。
//好像现在还没有现成的可以用。

import java.io.*;

/**
* An implementation of the FileFilter interfaces that filters files based
* on a DOS file name pattern. DOS file name patterns are case-insensitive
* and make use of the ? and * wildcard characters.
*/
public class MYFileFilter implements FileFilter{

private char[] pattern;

/**
* Creates a new DOSFileFilter with the provided pattern. The
* toMinimalPattern() method is implicitly called on the provided
* pattern.
*/

public MYFileFilter(String s){
s = s.trim();
s = toMinimalPattern(s);
s = s.toUpperCase();
pattern = s.toCharArray();
}

public boolean accept(File pathname){
String s = pathname.getPath();
s = s.toUpperCase();
char[] ca = s.toCharArray();
return match(pattern,0,ca,0);
}

/**
* Converts the provided pattern to it's minimum equivalent. Basically
* this method converts all touching *'s to a single *.
*/

public static String toMinimalPattern(String s){
int len = s.length();
StringBuffer sb = new StringBuffer();
int x = 0;
int y = s.indexOf('*');
while (x <= y){
y++;
sb.append(s.substring(x,y));
x = y;
while (y < len && s.charAt(y) == '*'){
y++;
x = y;
}
y = s.indexOf('*',x);
}
sb.append(s.substring(x));
return sb.toString();
}

/**
* Checks to see if a provided file name matches the provided DOS pattern.
*/

public static boolean match(String pattern,String fileName){
pattern = pattern.trim();
pattern = toMinimalPattern(pattern);
pattern = pattern.toUpperCase();
char[] ca1 = pattern.toCharArray();
fileName = fileName.trim();
fileName = fileName.toUpperCase();
char[] ca2 = fileName.toCharArray();
return match(ca1,0,ca2,0);
}

private static boolean match(char[] pattern,int px, char[] name, int nx){
while (px < pattern.length){
char pc = pattern[px];
if (pc == '*'){
if (px >= (pattern.length - 1))
return true;
int x = nx;
while (x < name.length){
if (match(pattern,px+1,name,x))
return true;
x++;
}
px++;
continue;
}
if (nx >= name.length)
return false;
if (pc != '?'){
if (pc != name[nx])
return false;
}
px++;
nx++;
}
return (nx >= name.length);
}

public static boolean isMatch(String mask, String nick) {
mask = mask.trim();
if (mask.equalsIgnoreCase("*")) {
return true;
}
if (mask.startsWith("*")) {
int end = -1;
if (mask.indexOf("*",mask.indexOf("*") + 1) == -1) {
end = mask.length();
} else {
end = mask.indexOf("*",mask.indexOf("*") + 1);
}
if (end == -1) {
return false;
}
String tmask = mask.substring(1, end).trim();
if (nick.indexOf(tmask) == -1) {
return false;
} else {
mask = mask.substring(end).trim();
nick = nick.substring(nick.indexOf(tmask)).trim();
return isMatch(mask, nick);
}
}
int end = -1;
if (mask.indexOf("*") == -1) {
end = mask.length();
} else {
end = mask.indexOf("*");
}
if (end == -1) {
return false;
}
String tmask = mask.substring(0, end).trim();
if (nick.startsWith(tmask)) {
if (end == mask.length()) {
return true;
}
mask = mask.substring(end).trim();
nick = nick.substring(tmask.length()).trim();
return isMatch(mask, nick);
} else {
return false;
}
}
}

62,614

社区成员

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

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