《西农java英语实习题目》由会员分享,可在线阅读,更多相关《西农java英语实习题目(5页珍藏版)》请在金锄头文库上搜索。
1、Lab 4In this lab, you will practice how to use the following classes: l BufferedReader and BufferedWriterl BufferedOutputStream and BufferedIutputStream (Similar to BufferedReader and BufferedWriter, I am sure you can handle them.) l FileOutputStream and FileInputStreaml DataOutputStream and DataInp
2、utStreaml RandomAccessFilel ObjectInputStream and ObjectOutputStreamTask 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:
3、interfaceLoggable / Three log types: NFORMATION, WARNING, ERRORenum 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
4、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:02INFORMATIONTHIS IS A CONTENT STRING*2010-12-
5、25 14:45:02INFORMATIONTHIS 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 Buffer
6、edOutputStream 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:*
7、2011-11-28 15:06:57INFORMATIONDone, cost 573 without buffer*2011-11-28 15:06:57INFORMATIONDone, cost 9 with buffer*2010-12-05 15:07:04ERRORAccess 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 =
8、 System.currentTimeMillis();/ do somethinglongendTime = 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 fol
9、lows: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;