(i) Sub-section buffer stream
(1) It is more efficient to read one byte array at a time than one byte at a time.
Reading one byte array at a time is equivalent to constructing a buffer, and is there a more efficient flow than reading one byte array at a time?
BYTE buffer stream:
(2) byte buffered output stream:
Construction Mode:
Public Bufferedoutputstream (first in development): The default buffer size (large enough) to construct a byte buffered output stream object (OutputStream out)
Public Bufferedoutputstream (outputstream out,int size): Specifies the size of the buffer to construct the buffered output stream object
IllegalArgumentException-If size <= 0
How to write Data:
Write one byte at a time
Write (int by)
Write one part of a byte array at a time
Write (byte[] b, int off, int len)
(2) byte buffered input stream
- Public Bufferedinputstream (InputStream in): Default buffer size construct buffered input stream object
- Public Bufferedinputstream (InputStream in,int size): Specifies the buffer size to construct a buffered input stream object
- public int Read ()
- public int read (byte[] b,int off,int len)
When you make an input stream,
Two ways to read (read one byte at a time/read one byte at a time), only in one way, otherwise, an error will occur!
(3) How does the computer store Chinese?
Current platform default encoding set: GBK one Chinese two bytes
First byte: Must be a negative number
Second byte: Typically a negative number, which may also be a positive number, without affecting the result.
(4) Storing files
IO stream: Persistent storage (time consuming)
Databases: Persistent storage
Basic byte-stream
File byte input stream/file byte output stream
Efficient byte stream (buffered streams)
StringBuffer: Provides a string buffer (the string can be appended to the buffer continuously)
(5) Encoding and decoding
Encoding and decoding: Before and after the encoding format to be consistent!
Coding:
- Simple comprehension: What you can read---> What you don't understand
- Decoding:
- Something you can't read---something you can read.
- Example: Spy War movie
See you in the old place today.
Coding:
Today----> Bytes----> Binary data
Decoding: Binary Data---Decimal data---> Byte----> String
This
Encoding: Turning a string into a byte array
- Public byte[] GetBytes (): Platform default encoding Set (default is GBK)
- Public byte[] GetBytes (Charset Charset); " Specify encoding format
Decoding: Byte array---> String
- Public String (byte[] bytes): Using the Platform default encoding set (GBK)
- Public String (byte[] bytes,charset Charset): Decodes with the specified encoding format
(ii) Character stream
(1) using a byte stream to read one bytes at a time, will cause Chinese garbled--->java provides a character stream (specifically to solve Chinese garbled problem)
(2 character input stream: Reader
Character output stream: Writer
- Character output stream/character input stream: abstract class
- Use a subclass: Convert a Stream
How to construct character output stream
- Public OutputStreamWriter (OutputStream out): Constructs a character conversion output stream object using the default encoding format
Public OutputStreamWriter (OutputStream out, Charset CS): Constructs a character conversion output stream object using the specified encoding format
Composition of the conversion stream = Byte stream + encoding format (platform default/specified)
Convert stream object creation, long format, very cumbersome, Java---> Conversion Flow convenience class
Reader: Abstract class: Character input stream
- InputStreamReader (character conversion input stream: inputstream+ encoded format)
Convenient class: FileReader, this class can directly manipulate the file
Writer: Abstract class: Character output stream
- OutputStreamWriter (character conversion output stream: outputstream+ encoded format)
(3) character conversion input stream: InputStreamReader
- InputStreamReader (InputStream in): Constructs a character conversion input stream, the default encoding
- Public InputStreamReader (InputStream In,charset CS) Constructs a character conversion input stream that specifies the encoding
Character conversion input stream = byte stream + encoding format
(4-character input stream read data method:
- int read (char[] CHS): reads a character array
- int read (): reads a single character
(5) Character output stream write data function:
- public void write (int c): Write a single character
- public void Write (char[] cbuf): An array of uppercase characters
- public abstract void Write (char[] cbuf, int off, int len): Part of an array of uppercase characters
- public void Write (String str): Write String
- public void Write (string str,int off, int len): writes a portion of a string
What is the difference between the *flush and the Close method?
- Close: Closes the stream, closes the stream object and its associated resource file, and, after closing, can no longer manipulate the convection object, or there will be an exception
- Flush: Refreshes the stream, in order to prevent some files (picture file/audio file), missing, or not loaded into the Stream object, refresh the stream, or you can stream the object to manipulate
Character buffered input stream/character buffered output stream
Assorted Flow (Properties: Property collection class/merge stream/serialization serializable/memory operation Flow)
(c) Character Buffer stream
Provides a more efficient stream-to-character buffer stream in the character stream
- Character buffer input stream
- Character buffered output stream
(1) BufferedReader: Character buffered input stream
Construction method
- Public BufferedReader (Reader in) creates a buffered character input stream that uses the default size input buffer.
- Public BufferedReader (Reader in, int sz) creates a buffered character input stream that uses the specified size input buffer.
(2) Bufferedwrier: Text writes the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings
Construction method
- BufferedWriter (Writer out): Default buffer size constructs character buffered output stream object
- BufferedWriter (Writer out,int size): Specify the buffer size
(3) Character buffered output stream:
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.SequenceInputStream;
Import java.util.Enumeration;
Import Java.util.Vector;
/*
Sequenceinputstream Another way of structuring
Public Sequenceinputstream (Enumeration e)
- Merging multiple input stream objects
- A.txt+b.txt+c.txt--->d.txt
-
*/
public class SequenceInputStreamDemo2 {
public static void Main (string[] args) throws IOException { Br>//stringdemo.java+systemindemo.java+printwriterdemo.java--->copy.java file
// Define a collection vectorvector<inputstream> v = new vector<inputstream> ();//use InputStream to encapsulate files inputstream S1 = new FileInputStream ("Stringdemo.java"); InputStream s2 = new FileInputStream ("Systemindemo.java"); InputStream s3 = new FileInputStream ("Printwriterdemo.java");//Add the Stream object to the collection V.add (S1); V.add (S2); V.add (S3);//Unique Functions enumeration< Inputstream> en = v.elements ();//Create a merge Input stream object Sequenceinputstream sis = new Sequenceinputstream (en);// Create byte buffered output stream object Bufferedoutputstream bos = new Bufferedoutputstream (New FileOutputStream ("Copy.java"));//read one byte array at a time byte[ ] Bys = new byte[1024]; int len = 0; while ((Len=sis.read (bys))!=-1) {bos.write (bys, 0, Len); Bos.flush ();} Close resource Bos.close (); Sis.close ();
}
}
(5) standard input and output stream
- InputStream in = system.in
- PrintStream out = syste.out;
*JDK5, Java--->scanner (inputstream in)
- Keyboard entry
- 1) Scanner
- 2) BufferedReader inside package character conversion input stream, package system.in
Standard output stream
PrintStream PS = System.out;
Use BufferedWriter to pack System.out
(6) Object flow
Serialization: Storing an object as a stream into a text file or transferring objects in the network----> stream data Serialization stream (ObjectOutputStream)
Deserialization: Restores a Stream object in a text file or stream object in a network transfer to an object stream data---> Object deserialization stream (ObjectInputStream)
Java.io.NotSerializableException: Exception for the current class that does not implement serialization functionality
- Serializable: Interface has no constructor method, no fields, no method
Interface----> Tag interface
Custom classes must implement the interface serializable interface to implement serialization functions
- Class implements the serializable also means that he is a tag class
- Assuming that the previous operation was against the Peroson sequence of operations, a token preson.class---> Fixed ID 100 was generated
- name-100
- age-100
- The serialization is complete, and then there are some things in the person class that have been modified and added ToString ()
- Person.class---Fixed ID--200
(v) Attribute collection class
(1) Properties: Represents a persistent set of properties (Abbreviation: Property collection Class) extends Hashtable<k,v> map collection
- Can be saved in a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
- Public Properties (): Non-parametric construction
(2) Unique features of the attribute collection class:
- Public Object SetProperty (string key, String value): Adds a key and value to the list of attributes, and forces both to use string
- Public set<string> Stringpropertynames (): Traversed functionality
- public string GetProperty (string key) searches for properties in this property list with the specified key
Can be saved in a stream or loaded from a stream, only using the Property collection class
public void Store (Writer writer,string Comments): Saves the data in the collection in a text file (property collection)
public void load (Reader reader): Loading data from a text file into a property collection
(vi) Multithreading
(1) How do I implement multi-threaded threads?
To implement multithreaded programs, you need to turn on processes,
Opening a process requires creating system resources, but the Java language cannot create system resources
Only C/C + + can create system resources and create good system resources
Java provides a class: the Thread class
- To implement multi-threaded procedures:
- 1) subclass that declares a class as Thread
- 2) The subclass should override the Run method of the Thread class
- 3) Creation of the object of the thread class on the main thread that made the custom
Parallel and concurrency (high concurrency: MyBatis--->ibatis: semi-automated)
The strong logically, while referring to the same time period
The latter physically, at the same time, refers to the same point
The Mythread class is an execution thread class
and rewrite the Run method in the thread class
The run () method should be a time-consuming operation, IO operation/loop statement.
(2) The Thread class provides some methods
- Public final void SetName (String name): Name the thread
- Public final String GetName (): Gets the thread name
(3) Methods related to thread priority:
- Public final int getpriority () returns the priority of the thread.
- Public final void setpriority (int newpriority) The priority of the thread
- Thread has a default priority
- public static final int max_priority 10 maximum priority
public static final int min_priority 1 min-Priority
public static final INT norm_priority 5 default Priority
(4) Public final void Setdaemon (Boolean on): True when represented as a daemon thread
- Mark the thread as either a daemon thread or a user thread. When a running thread is a daemon thread, the Java virtual machine exits. (The daemon will not end immediately, it will execute for a period of time at the end)
- The method must be called before the thread is started.
(5) method
- Public final void Join (): Waits for the thread to terminate the Interruputedexception interrupt exception
- public static void sleep (Long Millis): Thread sleep is specified as a time millisecond value
- Throws Interruptedexception
- Two different?
Public final void Stop (), forcing the thread to stop executing. Will not execute (out of date), the method can use the
Break thread in public void interrupt (). Represents a state of a thread break
Interview questions
Difference?
Wait (): Wait () call, Immediate release lock (Sync lock/lock Lock)
Sleep (): Thread sleeps, call does not release lock
- Public final void Stop (), forcing the thread to stop executing. Will not execute (out of date), the method can use the
- public static void yield () pauses the currently executing thread object and executes other threads
Java Basics (ix)