📄 ourdateutility.java
字号:
import java.util.Date;import java.io.*;public class OurDateUtility{ /** * Static utility method that takes an instance of the Date class and a * file name and serializes the Date instance to the file */ public static void writeDateToFile( Date dateInstance, String fileName ) { try { // Create the object stream on an output file stream FileOutputStream fileStream = new FileOutputStream( fileName, false ); ObjectOutputStream objectStr = new ObjectOutputStream( fileStream ); // write out the object objectStr.writeObject( dateInstance ); // Close the resources objectStr.close(); fileStream.close(); } catch( FileNotFoundException ex ) { ex.printStackTrace(); System.out.println( "Could not create the file for some reason" ); System.exit(-1); } catch( IOException ex ) { ex.printStackTrace(); System.out.println( "There was a problem writing the object to the stream" ); System.exit(-1); } } /** * Static utility method that takes a file name and deserializes * a date instance from it and returns the Date instance */ public static Date readDateFromFile( String fileName ) { Date dateFromFile = null; try { // Create an Object input stream on an input file stream FileInputStream in = new FileInputStream( fileName ); ObjectInputStream objectInStr = new ObjectInputStream( in ); // read in the object Object obj = objectInStr.readObject(); // Double check that it is the right type before casting if ( obj instanceof Date ) { dateFromFile = (Date)obj; } } catch( ClassNotFoundException ex ) { ex.printStackTrace(); } catch( FileNotFoundException ex ) { ex.printStackTrace(); } catch( IOException ex ) { ex.printStackTrace(); } return dateFromFile; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -