20145317 Peng Yao "Java Program Design" 6th Week Study Summary Tenth chapter input/output 10.1 InputStream and OutputStream1, stream design concept
- Java abstracts the input/output into streams, the source and destination of the data, and the stream object that connects the two.
- From an application perspective, if you want to take the data out of the source, you can use the input stream, and if you want to write the data to the destination, you can use the output stream. In Java, the input stream represents the object as an Java.io.Inputstream instance, and the output stream represents the object as an Java.io.OutputStream instance.
- When you do not use InputStream and outputstream, you must use the close () method to close the stream. Because InputStream and Outputstrem operate the Java.io.Closeable interface, the parent interface is the Java.lang.AutoCloseable interface.
2. Streaming Inheritance Architecture
(1) standard input/output
- System.in and System.out look at the API files, they are InputStream and PrintStream, respectively, representing the standard input and standard output.
- You can use the Setin () method of the system to specify the InputStream instance and re-specify the standard input source. The standard output can be redirected to the document as long as the program is executed using > to direct the output to the specified document. You can use the system's SetOut () method to specify the PrintStream instance and output the result to the specified destination.
(2) FileInputStream and FileOutputStream
- FileInputStream is a subclass of InputStream, you can specify a file name to create an instance, once the document is created, and then it can be used to read data. FileOutputStream is a subclass of OutputStream, you can specify a file name to create an instance, once the document is created, and then it can be used to write data. Use Close () to close the document when not in use, whether FileInputStream or FileOutputStream.
- FileInputStream mainly operates the InputStream read () operation method, so that it can read data from the document, FileOutputStream the main operation of the OutputStream write () operation method, So that it can write data to the document.
(3) Bytearryinputstream and Bytearryoutputstream
- Bytearryinputstream is a subclass of Inputstrteam, you can specify a byte array to create an instance, and once created you can read a byte array as a data source. Bytearryoutputstream is a subclass of OutputStream, you can specify a byte array to create an instance, once you create a byte array as the destination to write the data.
- Bytearryinputstream mainly operates the InputStream read () operation so that it can read data from a byte array, Bytearryoutputstream mainly operates the write () operation method of OutputStream so that it can write data to a byte array.
3, Stream processing decorator
- InputStream, OutStream provides streaming basic operations, and if you want to do processing for input/output data, you can use a wrapper class. The commonly used packager has the function of buffer bufferedoutputstream, Bufferedinputstream, with data conversion processing DataInputStream, DataOutputStream, ObjectInputStream, ObjectOutputStream, etc. with the ability to serialize objects.
(1) Bufferedinputstream and Bufferedoutputstream
- Bufferedinputstream and Bufferedoutputstream mainly in the internal buffer function, operation and InputStream, OutputStream and not much difference.
(2) DataInputStream and DataOutputStream
- DataInputStream, DataOutputStream used to decorate InputStream, Outputstream,datainputstream, DataOutputStream, provide reading, Methods for writing Java basic data types, such as reading and writing int, double, boolean, and so on.
-Take a look at examples. The following member class can call Save () to store the data of the member instance itself, the file name is member's membership number, call Member.load () to specify the membership number, you can read the member data in the document. The code is as follows:
package cc.openhome;import java.io.IOException;import static java.lang.System.out;public class MemberDemo { public static void main(String[] args) throws IOException { Member[] members = { new Member("B1234", "Justin", 90), new Member("B5678", "Monica", 95), new Member("B9876", "Irene", 88) }; for(Member member : members) { member.save(); } out.println(Member.load("B1234")); out.println(Member.load("B5678")); out.println(Member.load("B9876")); }}
(3) ObjectInputStream and ObjectOutputStream
- ObjectInputStream provides the ReadObject () method to read data into an object, and ObjectOutputStream provides a writeobject () method to write the object to the destination. Objects that can be processed by these two methods must operate the Java.io.Serializable interface, which does not define any method, but is used as an indication that the object can be serialized. As shown in the example below, the result of Member2demo operation is the same as that of Memberdemo. As follows:
10.2 Character processing Class 1, reader and writer inheritance schema
For reading of character data, Java SE provides the Java.io.Reader class, which abstracts the source of character data read-in. The Java.io.Writer class is provided for writing to character data. It abstracts the destination that the data writes out. FileReader, FileWriter can read and write to the document, read or write by default using the operating system default encoding to do character conversion. When you start the JVM, you can specify-dfile.encoding to specify the encoding used by the FileReader, FileWriter.
2, character processing adorner
Just as InputStream and OutputStream have some adorner classes, reader and writer also have some adorner classes to use. If the byte data that is processed by the stream actually represents the encoded data of some characters, and you want to convert the byte data to the corresponding encoded character, you can use InputStreamReader, outputstreamwriter to package the streaming data. BufferedReader, BufferedWriter can provide buffer function to reader and writer, and it will be helpful to the efficiency when processing character input/output. Printreader and PrintStream are very similar in use, but in addition to OutputStream packaging, PrintWriter can also package writer, providing print (), println (), Format () and other methods.
The 11th chapter: threading and Parallel API11.1.1
Sometimes it takes a design program to have multiple processes, i.e. multithreaded programs. Class Operation Java.lang.Runnable interface, the access point is run (), and the thread instance executes the Run method defined by the Runnable instance, the boot thread must call the thread's Start method.
11.1.2
Methods for defining Threads:
- Extends Thread
Implements Runnable: Elastic
11.1.3
Deamon thread: If a thread is marked as Deamon, the JVM automatically terminates at the end of all non-deamon threads.
- Thread basic state diagram: High priority is performed first, otherwise the flow is executed.
- Placement Thread: The Join method, which completes the inserted thread before returning to the original thread.
Stop thread: Action depending on demand.
11.1.4Each thread belongs to a thread group and can define its own thread group.
11.1.5 in the case of race, the class that is out of the condition that is supposed to be controlled is a class that does not have thread safety.
Synchronized (the method must be locked for this instance): Add the Synchronized keyword to the method, or add it to the description sentence. If used improperly, it can cause a deadlock.
11.1.6Wait (), notify (), Notifyall () are methods that are defined by the object, which can be used to release the lock of objects through their control threads, or to notify the thread to participate in lock contention.
11.2.1
- Lock: You can lock () to get the object locked and unlock () unlocked. When calling Trylock (), if the lock does not occur and the block is not blocked, the Fouse is returned, regardless of whether the success or failure is unlocked.
- Rwadwritelock interface: Defines the read lock and write lock behavior to improve efficiency.
- Stampedlock: Used to manipulate optimistic reads, which are used to read more threads and write fewer threads. First judge, and then consider whether the real lock.
Condition: Get multiple Conditon instances, you can have multiple wait sets, improve efficiency.
Learning progress Bar
|
lines of code (new | /Cumulative)
Blog volume ( | new/cumulative)
Learning time (new/cumulative) |
Important growth |
Goal |
5000 rows |
30 Articles |
400 hours |
|
First week |
200/200 |
2/2 |
20/20 |
|
Second week |
300/500 |
1/3 |
18/38 |
|
Third week |
500/1000 |
1/4 |
22/60 |
|
Week Four |
300/1300 |
1/5 |
30/90 |
|
Week Five |
800/2100 |
1/6 |
30/120 |
|
Week Six |
600/2700 |
1/7 |
30/150 |
|
Resources
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
20145317 Peng Yao "Java Program Design" 6th Week study Summary