IO has specific classifications: there are specific classifications:
1: Different depending on the type of processing: byte stream and character stream.
2: Varies by flow: input stream and output stream.
============= (supplementary byte is distinguished from character concept) ==============================
Characters: Abstract entities that can be represented using a variety of different character schemes or code pages. For example, a Unicode UTF-16 encoding represents a character as a sequence of 16-bit integers, whereas a Unicode UTF-8 encoding represents the same character as a 8-bit byte sequence. The common language runtime uses Unicode UTF-16 (Unicode conversion format, 16-bit encoded form) to represent characters.
Bytes: bytes are units that transmit information over the network (or store information on the hard disk or in memory).
An English letter occupies a space of one byte, and a Chinese character occupies two bytes of space.
Symbol: English punctuation occupies one byte, Chinese punctuation is two bytes.
A binary number sequence, in a computer as a digital unit, typically a 8-bit binary number, such as an ASCII code is a byte, the conversion of such units is:
Characters people use the notation, a symbol in the abstract sense. ' 1 ', ' Medium ', ' a ', ' $ ', ' ¥ ', ...
Bytes A unit of data stored in a computer, a 8-bit binary number, is a very specific storage space. 0x01, 0x45, 0xFA, ...
==============================================================
The origin of the character stream:
Because of the different encoding of the file, there is an efficient manipulation of the character flow image. Principle: Actually is based on the word stream reading, to check the specified code table.
The difference between a byte stream and a character stream:
1, when the byte stream is read, it returns to one.
The stream of characters is read to one or more bytes using the byte throttle to check the specified encoding table and return the detected characters.
2, byte stream can handle all types of data slice MP3, AVI and so on.
A character stream can only handle characters data.
Conclusion: As long as it is plain text data, we should prioritize the character stream to deal with. The other is to use the byte stream.
----------------------------------------------------------------------
IO system with only two functions: Read and Write
How the byte stream is handled:
InputStream, OutputStream
How the character stream is handled:
Reader, Writer
. character stream and byte stream conversion
Features of the conversion stream:
- It's a bridge between a character stream and a byte stream.
- Converts the read byte data to a character by a specified encoding
- Converts the specified encoding of the read character data into bytes
When do I use a transform stream?
- When there is a conversion action between the byte and the character;
- The data for the stream operation needs to be encoded or decoded.
The specific object embodies:
- InputStreamReader: byte-to-character Bridge
- OutputStreamWriter: character-to-byte bridge
These two stream objects are members of the character system, they have transformations, and themselves are character streams, so it is necessary to pass in a byte stream object when constructing.
The flow at the beginning of the buffered is only added to the buffer to improve efficiency in reading and writing. Character stream can not be directly output, need to convert into a byte stream to output (this is really just know)!
There are three basic types of nodes in the Java 2 SDK: File, memory, Pipeline (pipe). File class
The file class is an object that encapsulates files and folders in the file system, and can manipulate files and folders through the idea of an object. The file class holds various meta-data information for files or directories, including file name, file length, last modified time, whether it is readable, gets the path name of the current file, determines whether the specified file exists, obtains a list of files in the current directory, and creates and deletes files and directories.
Randomaccessfile class
This object is not a member of the flow system, it encapsulates a byte stream, and also encapsulates a buffer (a character array), manipulating the data in a character array with an internal pointer. The object features:
- The object can only manipulate files, so the constructor receives two types of parameters: A. string file path; B.file object.
- The object can read and write to the file, and can specify the mode of operation when the object is instantiated (R,RW)
Note: When the object is instantiated, it is created automatically if the file to be manipulated does not exist, and if the file exists, the write data does not specify a location and is written from scratch, overwriting the original content. can be used for multithreaded downloads or for multiple threads to write data to a file at the same time.
----------------------------------------------------------------------
Read a text file and print:
1 PackageComz;2 3 ImportJava.io.File;4 ImportJava.io.FileReader;5 Importjava.io.IOException;6 7 Public classT {8 Public Static voidMain (string[] args)throwsIOException {9FileReader r =NewFileReader (NewFile ("D:\\a.txt"));Ten Char[] cs =New Char[500]; One intLen = 0; A intCount = 0; - while(len = R.read (cs))! =-1) { -count++; theSystem.out.println (NewString (CS, 0, Len)); - } - r.close (); - System.out.println (count); + } -}
When processing a character stream, a char array is used, and a byte array is used when processing the byte stream.
1FileInputStream FIS =NewFileInputStream (NewFile ("D:\\a.txt"));2 byte[] ss =New byte[1024];3 while(true) {4 intc =Fis.read (ss);5 if(c = =-1) {//reach End6 Break;7}Else {8System.out.println (NewString (ss));9 }Ten}
--------------------------------------
The byte stream can handle any file. For example, if we want to copy a picture, we can
1 ImportJava.io.BufferedInputStream;2 ImportJava.io.BufferedOutputStream;3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 ImportJava.io.FileReader;7 Importjava.io.IOException;8 9 Public classJavaio {Ten Public Static voidMain (string[] args)throwsIOException { One ABufferedinputstream bufin=NewBufferedinputstream (NewFileInputStream ("D:\\b.jpg")); -Bufferedoutputstream bufout=NewBufferedoutputstream (NewFileOutputStream ("D:\\c.jpg")); - the intByt=0; - while((Byt=bufin.read ())!=-1) - { - Bufout.write (byt); + - } + bufin.close (); A bufout.close (); at - } -}
Look at the source of Bufferedinputstream,
As you can see, although we did not set the buffer size, here is the default is 8,192 byte.
--------------------------------------
Noodles is a more interesting point.
---------------
In the Java IO System, in addition to the byte stream and character stream, there is the ability to convert the two to each other.
In particular, it is reflected in:
InputStreamReader and OutputStreamWriter
These are members of the character stream system.
They are a character stream, and they have a conversion function, so they have to pass in a byte stream as a parameter when constructing.
Look at the two constructors:
Their constructors are all passing in a byte stream as a parameter.
Of course, there are other constructors that can specify the encoding table, and so on.
The character stream object of the action file, which is a subclass of these transformation classes. Look at the following class structure:
The conversion stream has completed the work of the encoding transformation. Therefore, for the direct operation of the text file FileReader, there is no need to redefine it!
Therefore, it is important to note that when working with a text file, the FileReader is used, and the default encoding table is used. If you want to make your own coding table, you must use a transform stream.
For example, FileReader r = new FileReader (New File ("D:\\a.txt"));
The system default GBK encoding is used when manipulating this file.
If the data in the a.txt is encoded by UTF8, then it must be used like this:
InputStreamReader r=new InputStreamReader (New FileInputStream (New File ("D:\\a.txt")), "UTF-8");
----------------------------------------
Sequenceinputstream for merging multiple streams:
1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 ImportJava.io.InputStream;6 ImportJava.io.SequenceInputStream;7 Importjava.util.ArrayList;8 Importjava.util.Collections;9 Importjava.util.Enumeration;Ten One Public classSequenceinputstream { A Public Static voidMain (string[] args)throwsIOException { -Arraylist<inputstream> Al =NewArraylist<inputstream> ();//Byte stream - theAl.add (NewFileInputStream ("d:\\" + 2 + ". txt")); -Al.add (NewFileInputStream ("d:\\" + 3 + ". txt")); -Al.add (NewFileInputStream ("d:\\" + 1 + ". txt")); - +Enumeration<inputstream> en = Collections.enumeration (AL);//generates an old-fashioned enumeration for a parameter -Sequenceinputstream sis =NewSequenceinputstream (en);//Merge three data sources into one data source, byte stream + AFileOutputStream fos =NewFileOutputStream (NewFile ("D:\\5.txt")); at - intLen = 0; - byte[] bu =New byte[1024]; - while(len = Sis.read (BU))! =-1) { -Fos.write (BU, 0, Len); - } in - sis.close (); to fos.close (); + - } the}
--------------------
Let's take a look at Dateinputstream.
--------------------
Let's take a look at Dateinputstream.
One of the things that I did in the C # version of the internship was to use the stream to save things that were manipulated on the interface. The content is to save an int or double or a character for each time. Similar to the Dateinputstream function.
---------------------------------------------
These manipulate the array of stream objects, the source of the data is memory, and the purpose is memory.
So these streams do not need close when they are in use. The advent of these streams is actually a flow of thought to manipulate arrays.
-----------------------------
Coding issues
The default encoding for the Tomcat server is iso8859-1
-------------------------------
What encoding is used to encode the data, and what encoding method is needed to decode it?
IO Learning in Java