import java.util.*;
import java.io.*;public class Today{
public static void main(String[] args){
ObjectOutputStream out=null;
try{
out=new ObjectOutputStream(new FileOutputStream(new File("C:/date.dat")));
out.writeObject(new Date());
out.writeObject(new Long(System.currentTimeMillis()));//Object only
}catch(Exception e){
}finally{
try{
if(out!=null){out.close();}
}catch(Exception e){}
}
ObjectInputStream in=null;
try{
in=new ObjectInputStream(new FileInputStream(new File("C:/date.dat")));
Date date=(Date)in.readObject(); //读的顺序应与写时一致,并需转型
System.out.println(date);
Long millis=(Long)in.readObject();
System.out.println(millis);
}catch(Exception e){
}finally{
try{
if(in!=null){in.close();}
}catch(Exception e){}
}
}
}
import java.io.*;public class Today{
public static void main(String[] args){
ObjectOutputStream out=null;
try{
out=new ObjectOutputStream(new FileOutputStream(new File("C:/date.dat")));
out.writeObject(new Date());
out.writeObject(new Long(System.currentTimeMillis()));//Object only
}catch(Exception e){
}finally{
try{
if(out!=null){out.close();}
}catch(Exception e){}
}
ObjectInputStream in=null;
try{
in=new ObjectInputStream(new FileInputStream(new File("C:/date.dat")));
Date date=(Date)in.readObject(); //读的顺序应与写时一致,并需转型
System.out.println(date);
Long millis=(Long)in.readObject();
System.out.println(millis);
}catch(Exception e){
}finally{
try{
if(in!=null){in.close();}
}catch(Exception e){}
}
}
}
任务已经完成,真是十分感谢。
但我还有一个问题:System.currentTimeMillis()的作用是什么啊?
/**
* Returns the current time in milliseconds. Note that
* while the unit of time of the return value is a millisecond,
* the granularity of the value depends on the underlying
* operating system and may be larger. For example, many
* operating systems measure time in units of tens of
* milliseconds.
*
* <p> See the description of the class <code>Date</code> for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
public static native long currentTimeMillis();