1. Study summary 1.1 This week summarize multiple streams and file-related content in the way you like (mind map or other).
2. System-oriented integrated design-Library management system or shopping cart
Use streams and files to transform your library management system or shopping cart.
2.1 A brief description of how to use streams and files to transform your system. What is the format of the data in the file?
Using the object input stream and the object output stream to read and write the map of the saved information, the reader's borrowing information and book information are saved in the map, each time the information changes are re-written to the file, and then re-read.
Data format: Object stream writes to file is saved in binary, open with normal editor is garbled
2.2 Brief description of what interfaces and classes are used in the file reading and writing section of the system with the stream associated with the file? Why use these interfaces with classes?
Put the FileInputStream
wrapper ObjectInputStream
in and then you can read the stored map from
Put the FileOutputStream
wrapper ObjectOutputStream
in, and then you can write the map to the output stream
Book
Classes and Reader
classes implement Serializable
interfaces so that they can be serialized so that the map
2.3 Read and write file-related code. Critical lines need to be annotated.
Option: 3. Try to design a search engine system for the computer Academy website (Team completion)
You can start with the console only, not necessarily with the web.
Test data:
Demonstrate:
3.1 The system is probably divided into several parts, each part to complete what function?
A rough one, almost no algorithm involved, the method is placed in the same class, only the memory operation, no files involved
Search Content Format : id
(serial number), searKey
(search for keywords), details
(search content)
data storage: Map<id,details>
Save the serial number and content of a search, and Set[]
Save the word, that searchKey
is, the id
array corresponding subscript
The system is divided into 2 parts
① Adding search Content
② Search
3.2 What is the entire workflow of the system. What is the relationship between the various parts. You can try drawing description.
Add search content add(String id, String searchKey, String details)
, map.put(id,details)
call addSearchKey(String id, String searchKey)
, the search keyword after the word searchKey
is stored id
in the corresponding Set
subscript
Enter the search term--- getIds(String key)
method to get the search word participle after the corresponding Ids
(string save, id with ', ' separated), and the getDetails(String ids)
method returns a save the specific content of the List<String>
traversal outputList
3.3 What knowledge is needed to complete the system?
Mainly the use of collections, other knowledge fragmented use some.
3. Code volume Statistics 3.1 Statistics the amount of code completed this week
The weekly code statistics need to be fused into a single table.
Week Time |
Number of rows |
number of new rows |
Number of files |
number of new files |
1 |
226 |
226 |
45 |
45 |
2 |
377 |
377 |
7 |
7 |
3 |
712 |
281 |
42 |
9 |
4 |
166 |
166 |
2 |
2 |
5 |
253 |
253 |
4 |
4 |
6 |
484 |
484 |
9 |
9 |
7 |
269 |
269 |
1 |
1 |
8 |
301 |
32 |
5 |
4 |
9 |
447 |
447 |
7 |
7 |
10 |
401 |
26 |
9 |
0 |
11 |
1262 |
1262 |
18 |
18 |
12 |
965 |
965 |
12 |
12 |
Option: 4. Flow and Document Learning Guide (the job content is all selected at the bottom) 1. Character stream with text file: Use PrintWriter (write), BufferedReader (Read)
Writes the Student object (property: Int id, String name,int age,double grade) to the file Student.data, read from the file display.
1.1 Generated three student objects, using PrintWriter's Println method to write Student.txt, one student per line, and each student's attribute separated by |. Use scanner or BufferedReader to read the Student.txt data. (Key code, occurrence number)
1.2 What is the size of the generated file (using the right-click File Properties view)? Analyze the file size
1 character size 1 bytes, medium character size 2 bytes, carriage return Line 2 bytes, total 48 bytes.
1.3 If the println method of PrintWriter is called, but not close at the back. What is the file size? Why?
The close () method automatically calls the flush () method to write the buffer data to the file, not close, the data left in the buffer is not written to the file, and the file size is 0.
2. Buffer Stream 2.1 Use PrintWriter to write 10 million lines to a file (whatever it is), and then compare the speed (read only, not output) of using BufferedReader to read data from the file using scanner, which method is used fast? Intercept the test source code, the number of the study. Please analyze the reasons in detail. Tip: You can use JUNIT4 to compare run times
The use BufferedReader
of reading data faster, BufferedReader
is to read the data into the buffer and then write to main memory, the Scanner
data is directly written to main memory
2.2 Replace the PrintWriter with BufferedWriter to see if the speed of writing to the file has improved. Record the elapsed time of both. Try to analyze the reason.
The speed is significantly increased because the BufferedWriter uses a buffer
3. Character encoding 3.1 Existing EncodeTest.txt file, contains some Chinese, the file is encoded using UTF-8. Use FileReader and BufferedReader to read the EncodeTest.txt text into and out. Do you have garbled characters? Why does it have garbled characters? How to solve? (Key code, occurrence number)
There is garbled:
Java default encoding mode is GBK
, when reading UTF-8
the encoded file, Chinese will produce garbled
Use FileInputStream
the specified encoding format for UTF-8
resolution:
3.2 Writing method Convertgbk2utf8 (string src, string dst), you can convert the GBK encoded source file src to a UTF8 encoded destination file for DST.
void convertGBK2UTF8(String src, String dst){ BufferedReader br = null; try{ FileInputStream fis = new FileInputStream(src); InputStreamReader isr = new InputStreamReader (fis,"UTF-8"); br = new BufferedReader(isr); String line = null; PrintWriter printWriter = null; printWriter = new PrintWriter(dst); while((line = br.readLine())!=null){ printWriter.print(line); } printWriter.close(); }finally{ if(br!= null){ br.close(); } }}
5. Scanner Basic Concept Assembly object
Write public static List
7. File operation
Write a program, according to the specified directory and file name, search for the directory and all files under the subdirectory, if the specified file name is not found, then show no match, otherwise all the found file names and folder names are displayed.
7.1 Write the public static void FindFile (String path,string filename) function, using the path specified by path as the root directory, and recursively to find all the same file names under its directory and subdirectories as the filename. Once found, output to the console immediately. (Key code, occurrence number)
7.2 Use the queue, use the graphical interface, complete with Java nio.2 (optional 1)
Graphical interface:
201621123063 "Java program design" 12th Week experiment Report