代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class 实战4 {//类名为中文是可以的
//计数器,将运行程序的次数,上次运行程序的时间,用户上次运行的操作系统写入文本文件中。()
private static String input;
private static int count;
private static String time;
private static String information;
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\lenovo\\Desktop\\count.txt");
if(!file.exists()){
file.createNewFile();
}
//count= time=
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while((length=fis.read(buffer))!=-1){
input = new String(buffer,0,length);
}
if(input==null||input.trim().equals("")){
count = 0;
}else{
String[] arr = input.split(";");
count = Integer.parseInt(arr[0].substring(6, arr[0].length()));
System.out.println("上次程序运行的次数:"+count);
time = arr[1].substring(5, arr[1].length());
System.out.println("上次程序运行的时间:"+time);
System.out.println(arr[2]);
}
fis.close();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ss hh:mm:ss");
String time = sdf.format(new Date());
//获得系统的相关信息
Properties properties = System.getProperties();
String osName = properties.getProperty("os.name"); //操作系统名称
String osArch = properties.getProperty("os.arch"); //操作系统构架
String osVersion = properties.getProperty("os.version"); //操作系统版本
PrintWriter pw = new PrintWriter(file);
count++;
pw.write("count="+count+";");
pw.write("time="+time+";");
pw.write("information="+osName+" "+osArch+" "+osVersion);
pw.close();
}
}