license

本文介绍了一个Java程序,该程序用于验证文件中的时间戳,并根据当前时间和文件中记录的时间来判断文件的有效性。此外,程序还会定期更新文件中的时间戳以确保其有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值