The difference between java byte stream and character stream

Original address : https://www.cnblogs.com/DONGb/p/7844123.html

1. The concept of flow

All data in the program is transmitted or saved in a stream. When the program needs data, it must use the input stream to read the data, and when the program needs to save some data, it must use the output stream to complete.

The input and output in the program are all saved in the form of a stream, and what is saved in the stream is actually all byte files.

2. Byte stream and character stream

There are two main categories for manipulating file content in the java.io package: byte stream and character stream. Both are divided into input and output operations. The 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 the Reader class. (These four are abstract classes).

Java.io, a package dedicated to input and output functions, is provided in java, including: InputStream, OutputStream, Reader, Writer.

 InputStream and OutputStream, two are designed for byte streams, mainly used to deal with bytes or binary objects.

Reader and Writer. Two are designed for character stream (a character occupies two bytes), mainly used to process characters or strings.

The unit of character stream processing is a 2-byte Unicode character, which operates on characters, character arrays or strings, respectively, while the byte stream processing unit is 1 byte, which operates on bytes and byte arrays. Therefore, the character stream is formed by the Java virtual machine converting bytes into 2-byte Unicode characters as the unit of characters, so it has better support for multiple languages! If it’s audio files, pictures, or songs, it’s better to use byte streams; if it’s related to Chinese (text), it’s better to use character streams.

 The storage of all files is byte storage. What remains on the disk is not the characters of the file but the characters are first encoded into bytes, and then these bytes are stored on the disk. When reading a file (especially a text file), it is also read byte by byte to form a byte sequence.

The byte stream can be used for any type of object, including binary objects, while the character stream can only process characters or strings; 2. The byte stream provides the function of processing any type of IO operation, but it cannot directly process Unicode characters. The character stream is fine.

Byte stream is the most basic. All the subclasses of InputStrem and OutputStream are mainly used to process binary data. It is processed by bytes, but in reality, a lot of data is text, and the concept of character stream is also proposed. , It is processed according to the encode of the virtual machine, that is, the character set conversion is performed between the two through InputStreamReader, OutputStreamWriter to associate, in fact, through byte[] and String to associate Chinese characters in actual development In fact, it is caused by the inconsistent conversion between character stream and byte stream 

================== We can also see: ======= =====

The return type of the read() method of the Reader class is int: the character read as an integer (two bytes in total 16 bits), ranging from  0 to 65535  (0x00-0xffff), if the end of the stream has been reached, then Returns -1

Although the read() of inputStream also returns an int, since this type is byte-oriented, and a byte occupies 8 bits, it returns  an int byte value in the range of  0 to 255 . If there are no bytes available because the end of the stream has been reached, the value -1 is returned. Therefore, for values ​​that cannot be represented by 0-255, you must use a character stream to read! For example, Chinese characters.

3. Stream read and write operation flow

Operating procedures:

There are corresponding steps for IO operations in Java. Taking file operations as an example, the main operation flow is as follows:

a Use the File class to open a file

b Specify the location of the output by subclassing byte stream or character stream

c Perform read/write operations

d Turn off input/output

IO operation is a resource operation, you must remember to close it.

4. Specific use of byte stream

Byte stream mainly manipulates byte type data, which is subject to byte array. The main operation classes are OutputStream and InputStream.

Byte output stream: OutputStream, OutputStream is the largest parent class of the byte output stream in the entire IO package. The definition of this class is as follows:

public abstract class OutputStream extends Object implements Closeable,Flushable

From the above definition, we can find that this class is an abstract class. If you want to use this class, you must first instantiate the object through a subclass. If you want to operate on a file, you can use the FileOutputStream class. After up-casting, you can instantiate OutputStream

Closeable represents an operation that can be closed, because the program must be closed to the end

Flushable: means refresh, empty the data in memory

The construction method of the FileOutputStream class is as follows:

public FileOutputStream(File file)throws FileNotFoundException

Example of writing 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 InputStreamStudy {

  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);//如果文件不存在会自动创建

  10. String str="Hello World";

  11. byte[] b=str.getBytes();

  12. out.write(b);//因为是字节流,所以要转化成字节数组进行输出

  13. out.close();

  14. }

  15. }

Test screenshot:

It can also output byte by byte, as follows:

 
  1. import java.io.File;

  2. import java.io.FileOutputStream;

  3. import java.io.IOException;

  4. import java.io.OutputStream;

  5.  
  6. public class InputStreamStudy {

  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);//如果文件不存在会自动创建

  10. String str = "Hello World";

  11. byte[] b = str.getBytes();

  12. for (int i = 0; i < b.length; i++) {

  13. out.write(b[i]);

  14. }

  15. out.close();

  16. }

  17. }

Test screenshot:

The above output will only be overwritten, if you want to append, please see another constructor of the FileOutputStream class:

public FileOutputStream(File file,boolean append)throws FileNotFoundException

In the construction method, if the value of append is set to true, it means that the content is appended to the end of the file.

 
  1. import java.io.File;

  2. import java.io.FileOutputStream;

  3. import java.io.IOException;

  4. import java.io.OutputStream;

  5.  
  6. public class InputStreamStudy {

  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, true);//追加内容

  10. String str = "\r\nHello World";

  11. byte[] b = str.getBytes();

  12. for (int i = 0; i < b.length; i++) {

  13. out.write(b[i]);

  14. }

  15. out.close();

  16. }

  17. }

Run screenshot:

Change behavior in file:\r\n

Byte input stream: InputStream

Now that the program can write content to the file, it can read the content from the file through InputStream. First look at the definition of the InputStream class:

public abstract class InputStream extends Object implements Closeable

Like the OutputStream class, InputStream itself is also an abstract class and must rely on its subclasses. If you are reading from a file, use FileInputStream to implement it.

Observe the construction method of the FileInputStream class:

public FileInputStream(File file)throws FileNotFoundException

Examples of reading files:

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. InputStream in=new FileInputStream(f);

  7. byte[] b=new byte[1024];

  8. int len=in.read(b);

  9. in.close();

  10. System.out.println(new String(b,0,len));

  11. }

  12. }

Run screenshot:

But the above method is problematic. It is obviously a waste to open up such a large byte array. We can define the size of the byte array according to the size of the file. The method in the File class: public long length().

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. InputStream in=new FileInputStream(f);

  7. System.out.println(f.length());

  8. byte[] b=new byte[(int) f.length()];

  9. in.read(b);

  10. in.close();

  11. System.out.println(new String(b));

  12. }

  13. }

Run screenshot:

Let's change the way, read byte by byte~

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. InputStream in=new FileInputStream(f);

  7. byte[] b=new byte[(int) f.length()];

  8. for(int i=0;i<b.length;i++){

  9. b[i]=(byte) in.read();

  10. }

  11. in.close();

  12. System.out.println(new String(b));

  13. }

  14. }

Run screenshot:

But the above situation is only suitable for knowing the size of the input file, if you don't know, use the following method:

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. InputStream in=new FileInputStream(f);

  7. byte[] b=new byte[1024];

  8. int temp=0;

  9. int len=0;

  10. while((temp=in.read())!=-1){//-1为文件读完的标志

  11. b[len]=(byte) temp;

  12. len++;

  13. }

  14. in.close();

  15. System.out.println(new String(b,0,len));

  16. }

  17. }

Run screenshot:

5. Specific use of character stream

In the program, a character is equal to two bytes, then java provides Reader and Writer two specialized classes for manipulating character streams.

Character output stream: Writer.

Writer itself is a character stream output class, the definition of this class is as follows:

public abstract class Writer extends Object implements Appendable,Closeable,Flushable

This class itself is also an abstract class. If you want to use this class, you must use its subclass. At this time, if you want to write content to a file, you should use a subclass of FileWriter.

The construction method of the FileWriter class is defined as follows:

public FileWriter(File file)throws IOException

The character stream operation is better than the byte stream operation, that is, the character string can be output directly, and there is no need to perform the conversion operation as before.

Write file:

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator + "test.txt");

  6. Writer out = new FileWriter(f);

  7. String str = "Hello 字符流";

  8. out.write(str);

  9. out.close();

  10. }

  11. }

Run screenshot:

By default, the output will be overwritten again, and the method of appending is to add an append mark on the constructor:

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. Writer out=new FileWriter(f,true);//追加

  7. String str="\r\nHello 字符流";

  8. out.write(str);

  9. out.close();

  10. }

  11. }

Run screenshot:

Character input stream: Reader

Reader uses characters to retrieve data from files. The definition of the Reader class is as follows:

public abstract class Reader extends Objects implements Readable,Closeable

Reader itself is also an abstract class. If you want to read content from a file now, you can directly use the FileReader subclass.

The construction method of FileReader is defined as follows:

public FileReader(File file)throws FileNotFoundException

Read the data in the form of a character array:

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. Reader input=new FileReader(f);

  7. char[] c=new char[1024];

  8. int len=input.read(c);

  9. input.close();

  10. System.out.println(new String(c,0,len));

  11. }

  12. }

Run screenshot:

You can also use a loop to determine whether to read to the end:

 
  1. import java.io.*;

  2.  
  3. public class InputStreamStudy {

  4. public static void main(String[] args) throws IOException {

  5. File f = new File("d:" + File.separator+"test.txt");

  6. Reader input=new FileReader(f);

  7. char[] c=new char[1024];

  8. int temp=0;

  9. int len=0;

  10. while((temp=input.read())!=-1){

  11. c[len]=(char) temp;

  12. len++;

  13. }

  14. input.close();

  15. System.out.println(new String(c,0,len));

  16. }

  17. }

Run screenshot:

6. Summary

The use of byte stream and character stream are very similar, so besides the difference in operation code, what are the differences?

The byte stream itself does not use the buffer (memory) during operation, but directly operates with the file itself, while the character stream uses the buffer during operation.

When the byte stream is operating on the file, the file can be output even if the resource is not closed (close method), but if the character stream does not use the close method, nothing will be output, indicating that the character stream uses a buffer and can be used The flush method forces the buffer to be flushed, and then the content can be output without closing

Is it better to use byte stream or character stream in development?

When saving files or transferring on all hard disks, it is done in bytes, including pictures are also done in bytes, and characters are only formed in memory, so the operation using bytes is the most of.

If you want a java program to implement a copy function, you should choose a byte stream for operation (maybe the copy is a picture), and use the method of reading and writing (saving memory).

Guess you like

Origin blog.csdn.net/qq_30764991/article/details/103572619