在运行程序之前列出已修改的文件 [英] List out the files that has been modified before run the program
本文介绍了在运行程序之前列出已修改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好。目前我有一个代码可以列出目录中最新修改的文件。下面是我的代码:
Hi. Currently I have a code that can list out the latest modified file in the directory. Below is my code:
//store the file to the list
List<File> list = new ArrayList<File>(Arrays.asList(store));
//combine all the file at main folder and sub folder
Object[] combine = list.toArray();
//sort the file according to the date
Arrays.sort(combine, new Comparator<Object>(){
public int compare(Object o1, Object o2){
return compare((File)o1, (File)o2);
}
public int compare( File f1, File f2){
long StartTime = f2.lastModified() - f1.lastModified();
if( StartTime > 0 ){
return 1;
}else if( StartTime < 0 ){
return -1;
}else {
return 0;
}
}
});
//get the file name that has latest modified date
files = ((File) combine[0]).getName();
//get the file date that has latest modified date
lastModifiedDate = new java.util.Date(((File)combine[0]).lastModified()).toString();
System.out.println("The latest modified file is : "+files);
System.out.println("The time for the latest modified's file is :");
System.out.println(lastModifiedDate);
}
}
当我修改目录中的文件并运行程序时,它可以列出我修改过的文件。但是当我在目录中修改了超过1个文件并运行程序时,它只能显示具有最新修改时间的文件。
我的问题是:如何列出所有文件在运行程序之前是否已被修改?
When i modified a file in the directory and run the program, it can list out the file that i has been modified. But when i modified more then 1 files in the directory and run the program, it can only show the file that has the latest modification time.
My question is: How can i list out all the files that has been modified before i run the program?
推荐答案
上次修改日期不准确,我可以将其设置为我喜欢的任何内容。对于实际使用,请使用SHA-2等加密函数。
MessageDigest,DigestInputStream等将为您提供帮助。
Last modified dates cannot be accurate, I can set it to anything I like. For real-world uses, use cryptographic functions like SHA-2.
MessageDigest, DigestInputStream etc. will help you.
Hashmap<string,string> map = new Hashmap<>();
//Populate the map with checksums while load or better while compile
//map.put("filea","checksum");
public boolean checkModified(String file) {
File f = new File("some file");
MessageDigest md = MessageDigest.getInstance("SHA-2");
try(FileInputStream fin = new FileInputStream(file)) {
byte[] buffer = new byte[1024]; //buffer
int read = 0;
while((read = fin.read(buffer,0,1024)) != -1)
{
md.update(buffer,0,read);
}
String digest = toHex(md.digest());
//check against a prepopulated list of hashes
return map.get(f.getName()).equals(digest); //some say that message digests should be compared bitwise not in string form?
} catch (Exception e) {
//return false;
}
return false;
}
private static String[] hexArray = {
"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
"10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
"20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
"30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
"40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
"50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
"60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
"70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
"80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
"90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
"A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
"B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
"C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
"D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
"E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
"F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
String toHex(byte[] array)
{
StringBuffer sb = new StringBuffer();
for(byte b : array)
{
hexString.append(hexArray[0xFF & b]);
}
return sb.toString();
}
这篇关于在运行程序之前列出已修改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文