201621123037 Java programming 12th Week of study summary

Source: Internet
Author: User
Tags alphabetic character

Job 12-Stream and file

tags (space delimited): Java

1. Study summary 1.1 This week summarize multiple streams and file-related content in the way you like (mind map or other).

For:

Read operations

    • Read from File:

1. Byte Stream InputStream

File file = new File(“文件名”)InputStream f1 = new FileInputStream(file);

2. Character Stream BufferedReader

BufferedReader b= new BufferedReader(new FileReader(“文件名”));String str = b.readLine();

3.Scanner

Scanner in = new Scanner(new FileReader(“文件名”));while(in.hasNext()){    in.nextLine();   }
    • Read data directly from the console

      BufferedReader b= new BufferedReader(new InputStreamReader(System.in));Char ch = b.read(); //读取一个字符String str = b.readLine(); //读一行

Write operations

    • BufferedReader

      BufferedWriter b = new BufferedWriter(new FileWriter(“文件名”));b.write("wulalalalalalalala");b.newLine();
    • PrintWriter
      PrintWriter p = new PrintWriter(new FileWriter(“文件名”)); p.println("wulalalalalalalala");

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? 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? 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.

3.1 The system is probably divided into several parts, each part to complete what function? 3.2 What is the entire workflow of the system. What is the relationship between the various parts. You can try drawing description. 3.3 What knowledge is needed to complete the system? 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.

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)

For:

Write and read in the following:

Write file Result:

Read file Result:

1.2 What is the size of the generated file (using the right-click File Properties view)? Analyze the file size

For:

The resulting file size is 48 bytes.

Analysis:
Since each alphabetic character occupies one byte, the Chinese character occupies two bytes, and the carriage return line takes up two bytes, counting down exactly 48 bytes.

1.3 If the println method of PrintWriter is called, but not close at the back. What is the file size? Why?

For:

If close is not used for resource shutdown, the file content is 0 bytes.
Because Printwrite is to store content in the buffer, if there is no close to shut down, there is no way to write the contents of the buffer to the file, the data is lost. In addition to the Close method, using the Flush method is equally effective.

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

Write 10 million lines:

Write file Result:

Use BufferedReader to read data from this file using scanner:

JUNIT4 comparison:

For:
It is obvious that BufferedReader reads files faster.
Analysis:
BufferedReader reads fast because it simply reads the sequence of characters, and scanner has the ability to parse the data in a class.

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.

Code:


Results:

For:
After changing to BufferedWriter, the speed is improved, because BufferedWriter can arbitrarily set the buffer size, read the time is a character stream.

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, advent number) 3.2 Write method Convertgbk2utf8 (string src, string dst), you can convert the GBK encoded source file src to a UTF8 encoded destination file for DST.

Reference: Inputstreamreadertest.java and teaching ppt

    1. byte stream, binary file: DataInputStream, DataOutputStream, ObjectInputStream

4.1 Refer to the DataStream directory related code, try to write the data of three student objects to the file, and then read from the file and display. (Key code, occurrence number)
4.2 The file generated here and Topic 1 generated files why not the same? How large is the generated file? Parse the file size? Compare the file size to the file generated by topic 1 is it big or small, why? When it comes to storing data, is it easier to save space in binary files or text files? What are the benefits of using a binary storage file?
4.3 Use the Wxmedit 16 binary mode (or the 16 binary mode of other text editors) to open Student.data and analyze how the data is stored in the file.
4.4 Use ObjectInputStream (read), ObjectOutputStream (write) Read and write students. (Key code, Advent Number)//reference Objectstreamtest Directory

Reference: Topic specific requirements see Flow and Document Experiment Task Book-title 1-1

    1. Scanner Basic Concept Assembly object

Write public static List

Lab File: Students.txt
Reference: Textfiletest directory under Textfiletest.java

    1. Choice: Randomaccessfile

6.1 Using Randomaccessfile to achieve topic 1.1. (Key code, occurrence number)
6.2 Analyzing file sizes
6.3 Write a function public Student getstubyindext (int index), you can use Randomaccessfile to remove the student's information from the file according to the ordinal index. (The key code, the occurrence of the school number). And answer, where is the randomaccessfile of the random access characteristics of the file.

Reference: Randomfiletest.java under the Randomfiletest directory
Expand reading: How to use nio.2 to do random file access. Reference-random Access Files

    1. File operations

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)
7.3 Choose: Performance test, Test your findfile to find the speed of matching files. There are solutions that can make finding faster. such as everything-like search speed.
7.4 Optional: Implementation deletes the specified directory (if the directory is not empty, delete all files and directories under that directory and its subdirectories).

Reference code: Finddirectories.java
Reference: Topic specific requirements see Flow and Document Experiment task Book-Title 2
7.5 Select: Convert all. java files in the specified directory and subdirectories into UTF-8 encoded format and test.
Reference: Determining the encoding format of a file
7.6 Choose to do advanced: Fork in the Code statistics project, review code, and give advice. If possible, you can also add new features and pull Request

    1. Regular expressions

8.1 Use regular expressions to determine if a given string is a 10-digit format? Try programming to validate, to give the test data set and run the result (can be converted to PTA). (Key code, occurrence number)
8.2 Explain the regular expression you wrote.
8.3 Choose to do: Modify the Hrefmatch.java, try to match the number string in the Web page, match the picture string in the Web page. (can be converted to PTA)

Reference: Specific requirements of the topic see Flow and Document Experimental task book-Title 3
8.4 Choose to do (more difficult): further transformation of the above program, to obtain a link to the picture, such as img src= "Images/mail1.gif", and then processed, generate the picture of the actual link address http://cec.jmu.edu.com/images/mail1.gif. Finally, several addresses are generated and placed in a queue. Write the method, you can follow the queue of all the picture address, one time to download the picture.

References: Regular expression reference articles
5. Optional: Extracurricular reading

5.1 Trying to translate the summary in Lesson:basic I/O
5.2 Attempt to complete questions and Exercise
5.3 Character Set and encoding
5.4 Syntax and examples for Java regular expressions

201621123037 Java programming 12th Week of study summary

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.