Android 之文件夹排序
按文件名排序
/** * 按文件名排序 * @param filePath */ public static ArrayList<String> orderByName(String filePath) { ArrayList<String> FileNameList = new ArrayList<String>(); File file = new File(filePath); File[] files = file.listFiles(); List fileList = Arrays.asList(files); Collections.sort(fileList, new Comparator<File>() { @Override public int compare(File o1, File o2) { if (o1.isDirectory() && o2.isFile()) return -1; if (o1.isFile() && o2.isDirectory()) return 1; return o1.getName().compareTo(o2.getName()); } }); for (File file1 : files) { if (file1.isDirectory()) { FileNameList.add(file1.getName()); } } return FileNameList; }
基于名称:
/**
* 按文件名排序
* @param filePath
*/
public static ArrayList<String> orderByName(String filePath) {
ArrayList<String> FileNameList = new ArrayList<String>();
File file = new File(filePath);
File[] files = file.listFiles();
List fileList = Arrays.asList(files);
Collections.sort(fileList, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
if (o1.isDirectory() && o2.isFile())
return -1;
if (o1.isFile() && o2.isDirectory())
return 1;
return o1.getName().compareTo(o2.getName());
}
});
for (File file1 : files) {
if (file1.isDirectory()) {
FileNameList.add(file1.getName());
}
}
return FileNameList;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
基于最近修改时间:
/**
* 按文件修改时间排序
* @param filePath
*/
public static ArrayList<String> orderByDate(String filePath) {
ArrayList<String> FileNameList = new ArrayList<String>();
File file = new File(filePath);
File[] files = file.listFiles();
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f1.lastModified() - f2.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
}
public boolean equals(Object obj) {
return true;
}
});
for (File file1 : files) {
if (file1.isDirectory()) {
FileNameList.add(file1.getName());
}
}
return FileNameList;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
基于大小:
/**
* 按文件大小排序
* @param filePath
*/
public static ArrayList<String> orderBySize(String filePath) {
ArrayList<String> FileNameList = new ArrayList<String>();
File file = new File(filePath);
File[] files = file.listFiles();
List<File> fileList = Arrays.asList(files);
Collections.sort(fileList, new Comparator<File>() {
public int compare(File f1, File f2) {
long s1 = getFolderSize(f1);
long s2 = getFolderSize(f2);
long diff = s1 - s2;
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
}
public boolean equals(Object obj) {
return true;
}
});
for (File file1 : files) {
if (file1.isDirectory()) {
FileNameList.add(file1.getName());
}
}
return FileNameList;
}
/**
* 获取文件夹大小
* @param file File实例
* @return long
*/
public static long getFolderSize(File file) {
long size = 0;
try {
java.io.File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}