The usage example is as follows, use JAVA to run Sort
1. Enter the file path you want to sort. For example, the example is to sort the files under H:/ and the files under all its subfolders.
2. Enter the latest size that needs to be sorted and displayed. For example, the example is to sort files above 10M in size.
3. Sort from large to small and press
File path/file name-------Size KB--------Creation date displayed (yyyyMMdd)
format for display.
This way you can delete files that are too large and clear up space.
Running example: H drive points to my phone’s memory card
Copy the code code as follows:
D:/hjbsSorft/work/20140207/SortSize/bin>java com.he.jinbin.Sort
Enter the address of the file to be sorted: H:/
Enter the file size to be sorted (unit M): 10
Running, please wait...
The files are sorted from largest to smallest as:
H:/.android_secure/com.sg.android.fish-1.asec-------36224000 KB--------20130525
H:/BaiduMap/vmp/h/quanguogailue.dat-------27616013 KB--------20130512
H:/Download/RedGame_Android_2017-2013-11-06_18-54-27-CI-20.apk-------26563096 KB--------20131111
H:/ugame/ugameSDK/downloads/6F9757F4442DD99FC89FA387C80405D2.apk-------26305964KB--------20131025
H:/Download/com.tencent.mobileqq_60.apk-------25417880 KB--------20130714
H:/Android/data/com.android.gallery3d/cache/imgcache.0-------22070789 KB--------20140210
Copy the code code as follows:
package com.he.jinbin;
import java.util.Date;
/**
* Used to sort logical entity classes
*/
public class FileItem implements Comparable {
private String fileName;
private long size;
privateDatecreatTime;
public FileItem(String fileName, long size, Date creatDate) {
// TODO Auto-generated constructor stub
this.fileName = fileName;
this.size = size;
this.creatTime = creatDate;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public Date getCreatTime() {
returncreatTime;
}
public void setCreatTime(Date creatTime) {
this.creatTime = creatTime;
}
@Override
public int compareTo(Object o) {
if (this.size > ((FileItem) o).getSize())
return 1;
return -1;
}
}
Copy the code code as follows:
package com.he.jinbin;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* Main class used for sorting logic
*/
public class Sort {
public static List<FileItem> fileItems = new ArrayList<FileItem>();
public static FileItem maxFileItem;
public final static long M_1 = 1024 * 1024;
public static long temp = M_1; // Files larger than 1M by default
public static void addFileItem(File file) {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
file = fileList[i];
if (file.isDirectory()) {
addFileItem(file);
} else {
if (file.length() > temp) {
fileItems.add(new FileItem(file.getPath(), file.length(),
new Date(file.lastModified())));
}
}
}
}
public static void main(String[] args) throws IOException {
String filePath = null;
System.out.print("Enter the address of the file to be sorted:");
BufferedReader inRd = new BufferedReader(new InputStreamReader(
System.in));
filePath = inRd.readLine();
System.out.print("Enter the file size to be sorted (unit M):");
inRd = new BufferedReader(new InputStreamReader(System.in));
temp = Long.parseLong(inRd.readLine())*M_1;
inRd.close();
System.out.println("Running, please wait...");
File file = new File(filePath);
addFileItem(file);
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
Collections.sort(fileItems);
System.out.println("Sort files from largest to smallest:");
for (int i = fileItems.size() - 1; i >= 0; i--) {
FileItem item = fileItems.get(i);
System.out.println(item.getFileName() + "-------" + item.getSize()
+ " KB" + "--------" + fmt.format(item.getCreatTime()));
}
}
}