1.文件操作。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
public class fileOperator {
private static String filePath="txt.txt";
private static File file=new File(filePath);
private static int runCount=0;
private static String date=String.format("%tF %<tT", new Date());
private static String os=System.getProperty("os.name");
private static String dataStr="";
public static void main(String args[])
{
loadConfig();
if(dataStr.isEmpty()) System.out.println("First run!");
else
{
System.out.println("runCount: "+runCount+" date:"+date+" os: "+os);
}
putConfig() ;
System.out.println("Hello,java!");
}
private static void loadConfig()
{
try
{
if(!file.exists()) file.createNewFile();
byte[] data=new byte[64];
FileInputStream fis=new FileInputStream(file);
int rs=0;
while((rs=fis.read(data))>0) dataStr+=new String(data,0,rs);
if(!dataStr.isEmpty())
{
String[] sets=dataStr.split(",");
runCount=Integer.parseInt(sets[0]);
date=sets[1];
os=sets[2];
}
fis.close();
}catch(Exception e)
{e.printStackTrace();}
}
private static void putConfig()
{
//String dataStr;
try
{
if(!file.exists()) file.createNewFile();
dataStr=(runCount+1)+","+date+","+os;
byte[] data=dataStr.getBytes();
FileOutputStream fout=new FileOutputStream(file);
fout.write(data);
fout.flush();
fout.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}