Byte Stream and character stream
First look at a nasty concept:
All the data in the program is transmitted or saved in a stream, and the program needs the data to read the data using the input stream, and when the program needs to save some data, it will use the output stream to complete.
The input and output in the program is saved as a stream, and the actual bytes stored in the stream are all byte files.
Byte Stream and character stream
In the java.io package to manipulate the contents of the main two major categories: byte stream, character stream, two categories are divided into input and output operations. Output data in the byte stream is mainly done using OutputStream, the input is InputStream, the output in the character stream is mainly done using the writer class, and the input stream is mainly done using reader class. (These four are abstract classes)
The package java.io, which is dedicated to the input and output functions, is available in Java, including:
Inputstream,outputstream,reader,writer
InputStream and OutputStream, two are designed for byte streams, primarily for processing bytes or binary objects,
Reader and Writer. Two are designed for character streams (two bytes in a character) and are primarily used to handle characters or strings.
A character stream processes a cell that is 2-byte Unicode characters, manipulating characters, character arrays, or strings, and a byte-stream processing unit of 1 bytes, manipulating bytes and byte arrays. So a character stream is a character that is converted from a Java virtual machine to a 2-byte Unicode character, so it's better for multi-language support! If it is audio files, pictures, songs, use a word throttle better, if it is related to Chinese (text), with a character stream better
All of the files are stored in byte (byte) storage, and the characters that are not files on the disk are encoded into bytes, and then the bytes are stored to disk. When reading a file (especially a text file), it is also a byte-by-byte read to form a sequence of bytes
byte streams can be used for any type of object, including binary objects, while character streams only handle characters or strings; 2. Byte stream provides the ability to handle any type of IO operation, but it cannot handle Unicode characters directly , and a character stream can
Byte stream is the most basic, all the Inputstrem and OutputStream subclasses are, mainly used in processing binary data, it is processed by byte, but in fact, a lot of data is text, and put forward the concept of character stream, it is the virtual machine encode to deal with, That is, the conversion of the character set is related by Inputstreamreader,outputstreamwriter, in fact through byte[] and string. The problem of Chinese characters appearing in the actual development is actually caused by the transformation of the character stream and the byte stream.
Using character streams when working with files
Use byte stream when working with videos and pictures
================== we can also see: ============
The read () method of the Reader class returns the type int: The character read as an integer (two bytes total 16 bits), the range is between 0 and 65535 (0X00-0XFFFF), and returns 1 if the end of the stream has been reached.
The read () of InputStream also returns int, but since this class is byte-oriented and has 8 bits for one bytes, it returns an int byte value in the range of 0 to 255 . If no bytes are available since the end of the stream has been reached, the return value-1. So for values that cannot be represented by 0-255, a character stream is used to read it! such as Chinese characters.
Operation Flow
In Java, the IO operation also has the corresponding steps, take the file operation as an example, the main operation flow is as follows:
1 opening a file using the file class
2 Specify the location of the output by a subclass of Byte stream or character stream
3 for read/write operations
4 off input/output
IO operation is a resource operation, always remember to close
BYTE stream
Byte stream is mainly the operation of type data, byte array, the main operation class is OutputStream, InputStream
BYTE output stream: OutputStream
OutputStream is the largest parent of the byte output stream in the entire IO package, as defined in this class:
Public abstract class OutputStream extends Object implements Closeable,flushable
As you can see from the above definition, this class is an abstract class, and if you want to use this class, you first have to instantiate the object through a subclass, so if you are working on a file now, you can use the: FileOutputStream class. After you have transformed up, you can instantiate the OutputStream
Closeable represents an operation that can be closed, because the program runs to the end and is sure to close
Flushable: Indicates refresh, emptying of in-memory data
The FileOutputStream class is constructed as follows:
Public fileoutputstream (file file) throws FileNotFoundException
Write Data:
1 Import java.io.File;
2 Import Java.io.FileOutputStream;
3 Import java.io.IOException;
4 Import Java.io.OutputStream;
5
6 public class Test11 {
7 public static void Main (string[] args) throws IOException {
8 file F = new file ("D:" + file.separator+ "test.txt");
9 OutputStream out=new FileOutputStream (f);//If the file does not exist, it will be created automatically
Ten String str= "Hello World";
Byte[] B=str.getbytes ();
Out.write (b);//Because it is a byte stream, it is converted to an array of bytes for output
Out.close ();
14}
Flow Operation Law:
The reason to understand this rule is because there are too many objects, and the development does not know which object is appropriate. Therefore, only the following four points need to be clarified:
1, clear source and purpose:
Source: InputStream (Byte stream) Reader (character stream)
Purpose: OutStream (Byte stream) Writer (character stream)
2, clear whether the data is plain text data , &N Bsp & nbsp; For example, if plain text data: source Reader
Source: Is Reader Is Writer.
No InputStream
The purpose of the Writer is to set Spare Drive: File
No OutStream there is File Reader in=new FileReader ("path");
Here you can determine whether to use which system FileWriter out=new FileWriter ("path");
3, it is clear that the specific equipment needs efficient function (buffering Flow):
Source. Keyboard: system.in Hard disk: File Memory: Array network: Socket stream BufferedReader bufi= new Bufferedrea Der (FileReader ("path"));
Objective. Console: System.out hard disk: File Memory: Array network: Socket stream bufferedwriter bufw= new BufferedWriter (Fi Lewriter ("path"));
4, whether additional features are required:
Need to be efficient (buffer): Buffer
The difference between a byte stream and a character stream