Notes
121, Stream according to the function can be divided into Data listener flow (Sink stream, listeners: memory, pipelines, files) and process flow
(Processing Stream)
122. Before talking about Java streaming class, let's talk about how to generate a file object, which is a stream-independent class. The file object can be used to generate
and the files (and their path) or directory structure-related objects, because different systems may have different catalog result representations, so that
Use file-complete and system-independent purposes (using abstract path notation).
123, the File object generation method:
File (string path): Converts a string representing a path to an abstract path representation
File (String parent,string child):p arent represents the directory, the child represents files, child is not nullable
File (file parent,string child):p arent represents a directory of a file object that the child represents, and the child does not
Can be empty
124. File provides methods to test or examine a file object:
Boolean exists (): Returns True if the file or directory exists
Boolean isdirectory (): Returns True if it is a directory
File[] Listfiles (): Returns an array of file objects in the directory represented by the object, or null if not the directory
String[] List (): Ditto, but returns a string array
Long Length (): Gets the size of the file associated with the object, and if not, the return value is 0L
String toString (): Getting abstract path notation
String GetParent (): Part of the directory that gets the abstract path notation (not the last part)
String getName (): Gets the last part of the abstract path representation
125, change the method of a File object:
Boolean Renameto (File newName): Changes the path name represented by the current File object to the pathname represented by NewName
。 The return value is true if the change succeeds
Boolean mkdir (): Generates a new directory. In success, the return value is True
Boolean mkdirs (): Generates a new directory containing subdirectories that do not exist. If successful, the return value is True
Boolean delete (): Deletes the file or directory represented by the current file object, or null if it is a directory. If successful,
Returns True
126. Listfiles () with no parameters lists all the file names. Using file[] Listfiles (filenamefilter filter) over
Filter some special extensions or file names that contain certain characters. FilenameFilter is an interface that must generate a class that implements it to
Responsible for filtering the filename, which replaces the FilenameFilter unique method accept (). Boolean Accept (File dir,string
Name, and if returned to true, the currently checked file is included in the return array of Listfiles ().
127. The constructor function of the byte stream:
FileInputStream (String name): Open file name to read data
FileInputStream (File file): Opens files associated with file object file for reading data
FileOutputStream (String name): Open file name to write data
FileOutputStream (File file): Opens files associated with file object file to write data
FileOutputStream (String name,boolean Append): Opens file name to write data, if append is
True, the data that is written is added to the original file.
128, the constructor for byte arrays (byte array):
Bytearrayinputstream (byte[] buffer): generates an input stream for a byte array and specifies a byte array
Buffer of the stream for this input stream.
Bytearrayoutputstream (): generates an output stream of a byte array with a default initial size of 32 bytes for the buffer, if
The size of the buffer increases as needed
Bytearrayoutputstream (int size): Ditto, but allocates the initial size byte to the buffer.
129, some bytearrayoutstream commonly used methods:
int size (): Returns the size of this stream buffer
Byte[] Tobytearray (): Generates a new byte array and copies the contents of the stream to its own array.
String toString (): Converts the contents of a stream to a string object.
130, the combination of documents: Sequenceinputstream
Sequenceinputstream (InputStream head,inputstream tail): Generate a new input stream, read first
Head, after the tail
Sequenceinputstream (enumeration enum): Merges a new input stream with the stream in the enum according to the order, within the enum
Must be a InputStream object.
131, the vector function is like an array, but it can be changed according to the size of the need.
132, Pipeline (pipe) provides a method of communication between threads, due to IPC (inter-process communication, interprocess
or ITC (inter-thread communication, communication between threads), but it cannot perform communication between different hosts.
133. An input pipeline is used to receive data written by an output pipe, so a thread is responsible for sending
(PipedOutputStream object) data, while another thread is responsible for receiving (PipedInputStream objects) the data.
134, the pipeline data flow is one-way, you need to generate another set of input and output pipeline to be responsible for another direction of data transmission
Send.
135, if you want to send the pipeline output stream data to the pipeline input stream, you can use
PipedOutputStream (PipedInputStream Pipein) or
Connect (PipedInputStream Pipein)
140, the character stream class: 1, BufferedReader is a filter (extends FilterReader). Filter
To process and then output the data from the stream. The constructor functions are:
BufferedReader (Reader in): Generates a buffered character input stream, in a reader
BufferedReader (Reader in,int size): Generates a buffered character input stream and specifies the size of the buffer to be
Size
The common methods are:
String ReadLine (): reads a line of text (at the end of \ n or \ r), and the string object returned does not contain \ n or \ r, if
Reaches the end of the stream and returns null
When read () or ReadLine () using the buffered stream reads the data, the first attempt is to read from the buffer, if there is no number in the buffer
The data will be read into the buffer as much as possible for subsequent reads.
141, 2), LineNumberReader is also a filter, and it is also a buffer flow (extends BufferedReader), available
To record the number of rows read in, where the so-called row ends with \ n or \ r. Common methods:
int Getlinenumber (): Gets the current number of rows.
142, 3), Printerwriter is also a filter (Extendes filterwriter) that can be used to import output into a device.
143, I/O flow is characterized by the way they can be linked together through the flow, each flow class will perform special work and then upload the results
Next link, you can use the filter to link the stream together.
BufferedReader inbuffer=new BufferedReader (New FileReader (InFile));
When you use a stream link, you only need to close the outermost link stream, and the other linked streams will automatically close. The link you can use is the last stream (
Inbuffer), we cannot use the FileReader stream directly at this time. If you write:
FileReader filein=new FileReader (inFile);
BufferedReader inbuffer=new BufferedReader (Filein);
At this point, the Filein still exists, which may be caused by the negligence of the writing program, and there are two stream links that lead to the same source (InFile), which
There is a potential danger.
144. Random Access: Randomaccessfile, constructors
Randomaccessfile (File file,string mode): Where mode is used to specify the mode of access, available for r,w or RW
Randomaccessfile (String name,string mode): Name for system-related file names
Common methods:
Read: Boolean Readboolean (); byte ReadByte (); Char Readchar ();d ouble readdouble
(); float readfloat (); int readInt (); Long Readlong (); short readshort (); String ReadLine ()
Write: void WriteByte (Byte v); void WriteByte (String s); void Writechar (char v); void
WriteChars (String s); void writedouble (double v); void writefloat (float v); void Writeint (int
v); void Writelong (long v); void Writeshort (short v)
Methods associated with the current file location:
void Seek (Long POS): Moves the file pointer to the position of the POS (not negative) this is the value relative to the initial position of the file (
Initial value is 0)
Long Getfilepointer (): Gets the position of the current file pointer.
Long Length (): Gets the length of the file
145, file compression and Decompression methods: Zipinputstream (extends Inflaterinputstream) and Zipoutputstream
(Extends Deflateroutputstream)
146, write the file to the zip file steps:
1 generate the Zipoutputstream object associated with the zip file to be generated
2 A ZIP file often contains not only a compressed file, we will each to join the file called a zip portal, we
Use ZipEntry (String fileName) to generate these ZipEntry objects.
3) Use Putnextentry (ZipEntry entry) to add this zip entry to the zip file
4 Write the contents of the file to this zip file
5 use Closeentry () to end the current zip entry and continue to the next Zip entry
147, the compressed file from the zip file read out the steps:
1 generate the Zipinputstream object associated with the zip file to be read
2 use Getnextentry () to get the next Zip entry
148, we take objects from a stream called serialization (serialization), in order to complete the serialization of objects (write an object to guide
The stream is called serialization (serializing), and the object is read out of a stream as deserialization (deserializing).
149. The objects to be accessed must meet the following conditions:
Class needs to be public
Class needs to implement Serializable interface
If the data is not serializable or does not need to be written, declare it as transient
150. Java.io provides ObjectInputStream and ObjectOutputStream to access objects, as they and DataInputStream and
DataOutputStream have common interfaces, so they can also use the same method to read or write data. There are other ways to do it:
void WriteObject (Object obj): For ObjectOutputStream, writes an object to a stream
Object ReadObject (): For ObjectInputStream, the object is read from the stream.