public class Check extends Thread
{
//时间文件路径
private String path;
//license文件路径
private String license;
public void setPath(String path)
{
this.path = path;
}
public void setLicense(String license)
{
this.license = license;
}
/**
* 开始检测
*/
public void run()
{
File file = new File(path);
//如果文件不存在
if(!file.exists())
{
System.out.println("文件不存在,创建并写入数据...");
//将时间写入文件
writeTimeToFile(file);
}
//如果文件存在
else
{
//读取文件时间与当前时间
long datetime = readTimeFromFile(file);
long nowtime = getNowTime();
//如果当前时间小于从文件读取时间,那么文件内容不正确,时间被修改
if((nowtime - datetime) < 0)
{
System.err.println("时间被修改,退出系统...");
System.exit(0);
}
else
{
System.out.println("文件正确,重新写入使用时间...");
//将时间写入文件
writeTimeToFile(file);
}
}
//判断使用期限
trem();
}
/**
* 判断使用期限
*/
private void trem()
{
System.out.println("判断使用期限...");
//...
}
/**
* 从文件读取时间
* @param file
* @return
*/
private long readTimeFromFile(File file)
{
long datetime = 0;
try
{
InputStream in = new FileInputStream(file);
byte[] b = new byte[in.available()];
in.read(b);
String bStr = new String(b);
datetime = Long.parseLong(bStr);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(0);
}
return datetime;
}
/**
* 将时间写入文件
* @param file
*/
private void writeTimeToFile(File file)
{
try
{
OutputStream out = new FileOutputStream(file);
long datetime = getNowTime();
out.write((datetime + "").getBytes());
out.flush();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(0);
}
}
/**
* 得到当前时间
* @return
*/
private long getNowTime()
{
return new Date().getTime();
}
}
license
最新推荐文章于 2024-10-19 11:06:27 发布