资源描述
Lab 4
In this lab, you will practice how to use the following classes:
l BufferedReader and BufferedWriter
l BufferedOutputStream and BufferedIutputStream (Similar to BufferedReader and BufferedWriter, I am sure you can handle them.)
l FileOutputStream and FileInputStream
l DataOutputStream and DataInputStream
l RandomAccessFile
l ObjectInputStream and ObjectOutputStream
Task 1. Create a class MyLog and it implements one interface called Loggable. This class is used to read and write “MyLog.log” which contains: the current time, the log type and the content.
The definition of Loggable is as follows:
interfaceLoggable {
// Three log types: NFORMATION, WARNING, ERROR
enum TYPE {
INFORMATION, WARNING, ERROR
};
// Add one log, including log type and log content.
void addLog(Loggable.TYPE type, String logContent);
// Show all the logs in the log file.
void readLog();
}
The requirements are:
l When new logs are added using addLog(), they should be appended to the log file. For example:
l When using readLog() to show all the logs in the console, convert all the lower case characters into upper case characters for the contents of each log. For example:
******************************
2010-12-25 14:40:02
INFORMATION
THIS IS A CONTENT STRING
******************************
******************************
2010-12-25 14:45:02
INFORMATION
THIS IS A CONTENT STRING
******************************
Tips:
l Use BufferedReader and BufferedWriter to read and write the log file.
l To get the enum value, use:type.toString() in the addLog().
l Pay attention to the date format, you may use the class: SimpleDateFormat.
Task 2. Use FileOutputStream and BufferedOutputStream separately to write 1,000,000 random numbers (double) into a binary file (Numbers.dat) and compare the time expenses. Write the results and error messages into MyLog.log (it is the same log file as in the Task 1).
l The result is as follows:
l The content of the log file is as follows:
******************************
2011-11-28 15:06:57
INFORMATION
Done, cost 573 without buffer
******************************
******************************
2011-11-28 15:06:57
INFORMATION
Done, cost 9 with buffer
******************************
******************************
2010-12-05 15:07:04
ERROR
Access Deny!!!
******************************
l After changing the Number.dat into “Read only”,run the program again and add the following log.
Tips:
l To calculate the time using:
longstartTime = System.currentTimeMillis();
// do something
longendTime = System.currentTimeMillis();
long cost = endTime - startTime;
l Use DataOutputStream and writeDouble()to write data.
Task 3. Write code to search data from Numbers.dat by inputting the index of the data you are going to search. The result is as follows:
Tips:Use class: RandomAccessFile.
Task 4 Write an object of Student in to Student.txt, and read the object to show the following information:
class Student implements Serializable {
private static final long serialVersionUID = 1L;
int id;
int age;
String name;
String department;
public Student(int id, String name, int age, String department) {
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
}
展开阅读全文