Java Week 12--streams and files

Source: Internet
Author: User

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 flow and documentation 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? 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 do you want # # #用这些接口与类? 2.3 Read and write file-related code. Critical lines need to be annotated. 3. Code volume Statistics 3.1 Statistics the amount of code completed this week requires that the weekly code statistics be fused into a single table.

Option: 4. Flow and Document Learning Guide (the job content is all selected at the bottom) 1. Character streams and text files: using PrintWriter(写),BufferedReader(read) will StudentObject (property: Int id, String name,int age,double grade) write to File # # # #student. Data, read from the file display. 1.1 Generated three student objects, using PrintWriterOf printlnMethod is written to Student.txt, each line # # #一个学生, and each student's attribute is separated by |. Read the data using Scanner or # # #BufferedReader将student. txt. (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

A: The resulting size file is 48 bytes.

1.3 If you call PrintWriterOf printlnmethod, but not at the back close。 What is the file size? Why?

A: The file size becomes 0 bytes, because the close () method is used when the stream is closed, the flush () method is called during this process, writer and outputStream all buffers in the chain are flushed and the data is lost.

2. Buffer Stream 2.1 use PrintWriterWrite 10 million lines to the file (whatever it is), then compare the use of # # #BufferedReader与使用Scanner从该文件中读取数据的速度 (read only, not lose # # #出), 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

A: Using BufferedReader is faster because it has a larger buffer and can reduce the number of operations on the file when used.

2.2 Will PrintWriterInto BufferedWriterTo see if the speed of writing to the file has improved. Record the elapsed time of both. Try to analyze the reason.

A: Using BufferedWriter to write files is faster because of the use of buffering technology.

4. Byte stream, binary file: DataInputStream, DataOutputStreamObjectInputStream4.1 References DataStreamDirectory-related code that attempts to write data from three student objects to a file and then read it out and display it. (Key code, occurrence number)


The results of the operation are as follows:

4.2 The file generated here and Topic 1 generated files why not the same? How large is the generated file? Analysis # # #该文件大小? Compare the file size to the file generated by topic 1 is it big or small, why?

A: Larger than the file generated by topic 1, because using UTF-8 encoding to store files, Chinese characters accounted for a larger number of bytes.

Java Week 12--streams and files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.