关于一个文件数组的问题
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中 并以文件名排序 求指点
------解决方案--------------------
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;
}
}
------解决方案--------------------