本文实例讲述了Java判断时间段内文件是否更新的方法。分享给大家供大家参考。具体实现方法如下:
1.定时器
2.深化版
package classloader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author vma
*/
public class ManageClassLoader {
DynamicClassLoader dc =null;
Long lastModified = 0l;
Class c = null;
//加载类, 如果类文件修改过加载,如果没有修改,返回当前的
public Class loadClass(String name) throws ClassNotFoundException, IOException{
if (isClassModified(name)){
dc = new DynamicClassLoader();
return c = dc.findClass(getBytes(name));
}
return c;
}
//判断是否被修改过
private boolean isClassModified(String filename) {
boolean returnValue = false;
File file = new File(filename);
if (file.lastModified() > lastModified) {
returnValue = true;
}
return returnValue;
}
// 从本地读取文件
private byte[] getBytes(String filename) throws IOException {
File file = new File(filename);
long len = file.length();
lastModified = file.lastModified();
byte raw[] = new byte[(int) len];
FileInputStream fin = new FileInputStream(file);
int r = fin.read(raw);
if (r != len) {
throw new IOException("Can't read all, " + r + " != " + len);
}
fin.close();
return raw;
}
}
3.thread方法
希望本文所述对大家的Java程序设计有所帮助。
新闻热点
疑难解答