JAVA16
Io Flow Converter Introduction
L Chinese system default encoding table GBK
The construction method of L FileWriter assumes the default encoding GBK
l But some text files may be other ways of encoding
L So the introduction of the conversion stream can set the encoding table
L It's important to develop Internet programs afterwards
Overview
A kind of L character stream
L-Character and Byte bridge
L Inherit writer class
OutputStreamWriter
L java.io. OutputStreamWriter
L Inherit writer class
L character output flow to text file write data
L character stream is a byte flow
L graphic
OutputStreamWriter converts the text to a byte representation of the Set Code table and then writes it by FileOutputStream
l Example
- GBK
- UTF-8
L Use the default encoding table a little more
InputStreamReader
L bytes flow to character stream
L Inherit reader class
L graphic
- FileInputStream read a file one byte at a time
- One byte can not check the Code table
- So the conversion stream lets FileInputStream continue reading until the code table can be queried
- Convert stream query complete return character to program
l Example
- GBK
- UTF-8
?? Indicates that the GBK byte representation is not found in the UTF-8.
Conversion Stream and subclass differences
L Outputstreamwrite and InputStreamReader the bridge between the character and the byte when the required and default encoding table is different, you need to convert the two parent classes to the characters in the specified encoding or the representation of the characters and then input or output principle is the byte stream + encoding table The input or output of the byte stream is then used to find the corresponding character or negative number through the encoding table.
L sub-class no conversion function convenient operation does not require the encoding table directly write a file path default encoding table can be
L need to use parent class once you need to develop additional coding
Byte Buffer Stream Introduction
L Stream Object Read file
L need to use arrays when reading large amounts of data
Sun Company also has a solution
L provides a set of buffered streams for increased efficiency
L Buffered Opening
BYTE buffer stream Bufferedoutputstream
L Implement buffered output stream applications can write individual bytes to the underlying output stream without having to call the underlying system for each byte write
L graphic
- Write a string of data one by one write too slow because the stream itself does not process each time it is given to the system to process this will consume too much resources
l Example
- Java.io.BufferedOutputStream
- Increase the write efficiency of the original output stream
- Inherit OutputStream
- Method can write bytes and byte arrays
- The constructor method parameter is a byte output stream
Bufferedoutputstream (OutputStream out)
- Can pass any byte output stream
- Which byte stream is passed to improve efficiency
Bufferedinputstream
L Inherit InputStream
L can read bytes and arrays
L and keyboard input about
L system.in in is InputStream type
L Output is
The subclass object is Bufferedinputstream
L System.out sub-class objects when PrintStream
l Example
Comparison of four types of file copying methods
L Byte stream reads and writes individual bytes for replication
L Byte stream reads and writes bytes array for copying
L byte stream buffer reads single bytes
l byte buffer read/write byte array
Character output buffered stream BufferedWriter
L Java.io.BufferedWriter
L Inherit writer
Efficient writing of L characters
Writing method write () can be a single character array string
L Construction Method BufferedWriter (Writer W) pass any character output stream
L FileWriter OutputStreamWriter
l Example
L has a unique approach.
- void NewLine ();
\ r \ n
NewLine () method
- Select newline ()
- Because the method has platform independence, it can be cross-platform
- Windows \ r \ n
Linux \ n
- System.out.println (); The newline println is the newline () method
BufferedReader
L Inherit Reader
L Sub-class Linenumreader
L read text from the character input stream buffer individual characters for efficient reading of character arrays and rows
L have unique function to read a line in text String readLine ()
L can read an array of characters and characters
L String ReadLine ()
- Encountered \ r \ n the end of line
- The string that returns the content does not contain a terminator
- Null is returned at the end of the read
L LineNumberReader Subclass reads line numbers
l Example
- The constructor method parameter can be any character input stream
- FileReader InputStreamReader
- Unique feature String ReadLine ()
- The Get content method generally has a return value
Int If none of the returns are negative
Reference type could not find return null
Bollean cannot find the return is False
- Loop read
- ReadLine () returns a string that includes a newline symbol if printing removes ln
- Line number
- There is a subclass linenumberreader that can implement line numbers
Character buffer stream copy text file
Two more bytes.
Duplicate a blank line in the past
Because the loop is executed at the end of a readline ()
The Operation Law of flow
L How to select a Stream object
Whether you are manipulating data sources or data purposes, even if you are writing data to a program or from a program
L text or byte to manipulate
• Specific equipment where the data is defined
Memory
Internet
Socket in socket network both machines have the ability to interconnect successfully with a byte stream
L Need additional functionality
All Stream objects
|
BYTE stream
The input stream reads from the specified file
Efficient streaming set buffers for efficient reading
Output stream writes data to the specified file
Efficient stream set buffer efficient write
Character Stream
Output byte conversion stream output a negative representation of the character in the specified encoding that needs to be written to the file
Output stream writes data to the specified file
Efficient stream set buffer efficient write
Input byte conversion stream the number of characters to be read by the input program in the specified encoding in a negative representation
The input stream reads from the specified file
Efficient streaming set buffers for efficient reading
|
InputStream
Properties Class Introduction
L Hashtable a subclass in the collection
L Hashtable is the implementation class of the Map interface
L Hashtable Sub-class
L No generics
L The methods in the Map collection can be used
L is a set of properties that can be persisted
The L key and value can be stored in the collection or stored in a persistent device. The source of the key value can also be a persistent device
L A method of combining with flow technology
Load and Store
L Java.util
L can be used in conjunction with IO objects to achieve persistent storage of data
Method
- SetProperty (String key,string value) is equivalent to a put
- GetProperty (String key) is equivalent to get
- Stringpropertynames stores the keys in the collection to the set collection equivalent to keyset
- Unique function load
- The load (InputStream in) load (reader R) Stream object reads the key-value pairs in the file that are saved in the collection
File
Example
#注释
- Store (OutputStream out) store (write W) store (write W, String comment)
Comment Description
- The output stream that receives all bytes or characters stores the key-value pairs in the collection into a file
Example
Comment (The reason for writing the file) is not recommended to write in Chinese because it will be written as a UTF-8 bytecode
Can write English or not write
After development can write the class name very practical
Serialization of objects and serialization of deserialization
The data in the L object is written as a stream to the file to save
The l process is called writing out the serialization of an Object object
L Write object is byte stream
L ObjectOutputStream Object Output byte stream
Deserialization
L read an object from a file as a stream in a file
L process is called deserialization of an object
L ObjectInputStream
ObjectOutputStream Stream Write Object
L Java.io.ObjectOutputStream
L write Java basic data types and graphics to OutputStream
L construction method requires a byte output stream outputstream
L pass an arbitrary byte output stream
l void WriteObject (Object obj) methods for writing out objects
l Example
An exception occurred
Serializable is an empty interface, no method.
No exception was successfully written after implementing interface
ObjectInputStream Stream Read Object
L Create a byte input stream object first
L then create the deserialized stream object incoming byte input stream object
L Construction Method
ObjectInputStream (InputStream in)
L Method ReadObject ()
The object is written as an object, and the object is read, so a strong turn is needed.
l Example
ReadObject throws two exception IO exceptions and the class cannot find an exception
If you delete the class file, you will be quoted as being unable to find an exception.
Static cannot be serialized
L If the member variable in the object is changed to static
l Example
Write first
Re-read
A GE is zero
Normal output is not zero
Because a static variable does not belong to an object, the class is shared data because the member variable has a default value so it is 0
Transient keywords
L Transient Keywords
• Member variables cannot be serialized when modified
L only use this one.
Serializable
L Identity Interface
L for identification of serialization
L do not have any functions
L Cloneable is also the identity interface cloning interface
Serial number conflict in serialization
L Write Object serialization first
L Modify the source code
L to read again
L have an exception
- ... UID is not the same
The UID of the compiled class file stream and the local class file UID are not the same as serial number collisions
L principle
- source files. java
- JVM compiled as a. class file
- The compiler calculates a sequence number in the class file based on the definition of the
- class file includes source code and serial number
- ObjectOutputStream Write Object writes serial number and variable to file
- Change the source code sequence number
- ObjectInputStream Read object reads the serial number and compares the serial number of the local class to read successfully or fails
- So there's a serial number conflict exception.
• Conflict resolution
- The serial number is not recalculated even if the source code is modified
- Need to make a lifetime constant serial number
- Copy and paste
- From
Copy and paste
Once determined cannot be modified otherwise equals invalid
Print Flow characteristics
L PrintStream byte print stream printwriter character print stream
L Inherit OutputStream
L never throw IOException but may throw other exceptions
L added functionality for other output streams
L Common PrintWriter
L Two class methods are the same
L But the constructors are different.
The construction method is the output destination of the print stream.
PrintStream
Receive file type OutputStream string file name
PrintWriter
Receive file type string file name OutputStream character output stream writer
Method
L Print println
l Example
println Output write query code table
Output statement is a char array
Only char arrays can print out elements
The method does an array traversal.
The other array only turns the reference object into a string
L Why
Because
If you don't traverse the string "abc", you can't print it.
The print stream output is intended to be a string and stream object
In the construction method is a string
The construction method is a stream object.
- BYTE stream
- Character Stream
Print stream turn on auto refresh
L The output must be a stream object
L must call one of the methods in println printf format to use the
L
l Example
Flush is not required but must be close
L Extension
You can convert other objects to stream objects and then refresh automatically or
Print Stream Copy text
L read Data source cannot be used for print stream
L Write data purposes you can use the print stream and automatically refresh
l Example
Commons IO Tool Class Introduction
Download Apache website download
L Directory Introduction
- The jar package used
- Help documentation
- Source
- Test source
Use
L Import Jar Package
L Import Source Code
Filenameutils
The L methods are static.
GetExtension
L Gets the extension of the file name returns the extension of type string
l Example
Source
GetName
L Get File name
l Example
Isextension
L determine if the filename suffix is a specified suffix
L returns True and False
l Example
Source
Case sensitive
FileUtils
L Many functions
L static method
L Direct Use
Static String readfiletostring (file file)
L receive the File object
L directly read the file returns a string
l Example
static void Writestringtofile (file file, String data)
L Write a string directly to a file
Static Long CopyFile (File src,file desc)
L Copy Files
l Example
static void Copydirectorytodirectory (file srcdir, file DestDir)
L Copy Folder
l Example
The Java self-study road-DAY16