关于一个文件数组的问题

wo111180611 2013-04-27 02:02:59

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

public class moveFile {

private static String oldPath = null;
private static File [] files = null;

public static void main(String[] args) {
oldPath = "D:\\迅雷下载";
findFile(oldPath);
}

public static void findFile(String path) {
File f = new File(path);
File[] fileNames = f.listFiles();

for (File child : fileNames) {
if (child.getName().matches("^jia.*"))
//这里怎么写
}
}
}

我想把文件夹下所有以"jia"开头的文件全部放到文件数组files中 并以文件名排序 求指点
...全文
219 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zting125240 2013-04-27
  • 打赏
  • 举报
回复
不要存放 File文件 ,将文件名保存在数组或集合中
LCore 2013-04-27
  • 打赏
  • 举报
回复
引用 楼主 wo111180611 的回复:

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

public class moveFile {

	private static String oldPath = null;
	private static File [] files = null;

	public static void main(String[] args) {
		oldPath = "D:\\迅雷下载";
		findFile(oldPath);
	}

	public static void findFile(String path) {
		File f = new File(path);
		File[] fileNames = f.listFiles();

		for (File child : fileNames) {
			if (child.getName().matches("^jia.*"))
				//这里怎么写
		}
	}
}
我想把文件夹下所有以"jia"开头的文件全部放到文件数组files中 并以文件名排序 求指点
 /**获取文件列表,并进行一些过滤操作!*/
	public static File[] getFiles() {
		if (contentPath == null) {

			File file = new File(filePath);
			fileList = new File[1];
			fileList[0] = file;
			return fileList;
		}
		fileList = new File(contentPath).listFiles(new FileFilter() {
			
			/**使用过滤器过滤掉目录*/
			@Override
			public boolean accept(File pathname) {
				if(pathname.isDirectory())
				{
					return false;
				}else if(pathname.getName().startsWith("jia"))
				{
					return false;
				}
				else
					
					return true;
			}
		});
		return fileList;
	}
至于文件名的比较,使用如下方式:
/** 对当前目录下的所有文件进行排序 */
	public static File[] sort() {
		getFiles();
		Arrays.sort(fileList, new FileComparator());
		return fileList;
	}
       // 提供一个"比较器"
	static class FileComparator implements java.util.Comparator<File> {
		@Override
		public int compare(File o1, File o2) {
			// 按照文件名的字典顺序进行比较
			return o1.getName().compareTo(o2.getName());
		}

	}
更加详细见博客: http://blog.csdn.net/kiritor/article/details/8857376
Foxeagle 2013-04-27
  • 打赏
  • 举报
回复
import java.io.File; import java.util.Arrays; public class MoveFile { private static String oldPath = null; private static File[] files = null; public static void main(String[] args) { oldPath = "D:\\迅雷下载"; findFile(oldPath); } public static void findFile(String path) { File f = new File(path); File[] fileNames = f.listFiles(); files = new File[fileNames.length]; int i = 0; for (File child : fileNames) { if (child.getName().matches("^jia.*")) { files[i++] = child; } } FileCompare[] fcs = new FileCompare[i]; for (int j = 0; j < i; j++) { fcs[j] = new FileCompare(files[j]); } Arrays.sort(fcs); for (int j = 0; j < i; j++) { files[j] = fcs[j].getFile(); System.out.println(files[j].getName()); } } } class FileCompare implements Comparable { private File file; public FileCompare(File file) { this.file = file; } public int compareTo(Object o) { FileCompare fc = (FileCompare) o; int BigOrSmall = this.file.getName().compareTo(fc.getFile().getName()); if (BigOrSmall > 0) { return 1; } else if (BigOrSmall < 0) { return -1; } else { return 0; } } public File getFile() { return this.file; } }

62,620

社区成员

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

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