Byte stream and character stream of Java IO and its difference

Source: Internet
Author: User

1. The concept of byte stream and character stream
1.1 Byte stream is inherited from InputStream OutputStream,
1.2 character streams inherit from InputStreamReader OutputStreamWriter.
There are many other streams in the java.io package, mainly to improve performance and ease of use.

2. The difference between a byte stream and a stream of characters
2.1 To one piece of binary data data output to a device, or from a device to read one piece of binary data, no matter what input is, we want to use a unified way to complete these operations, in an abstract way to describe, the abstract description of the method named Io stream, The corresponding abstract classes are OutputStream and inputstream, and different implementation classes represent different input and output devices, all of which operate on bytes.

2.2 In the application, often to be completely character of a piece of text to be sent out or read in, with a byte stream can it? Everything in the computer ends up being binary bytes. For the "China" characters, the corresponding bytes are first obtained, and then the bytes are written to the output stream. Read, the first read is the byte, but we want to display it as a character, we need to convert the bytes into characters. Because of such a wide range of needs, people specifically provide a character stream of packaging classes.

2.3 The underlying device will always accept only byte data, sometimes to write strings to the underlying device, to convert the string into bytes and then write. The character stream is the wrapper of the byte stream, and the character stream is directly accepting the string, which internally turns the strings into bytes and writes to the underlying device, which provides a little bit of convenience for us to write or read the string to the IO setting.

When converting characters to bytes, be aware of the encoding problem, because the string is converted into a byte array,
In fact, the character is converted into some kind of encoded byte form, reading is the opposite of the truth.

Case of byte-stream relationship with character flow:
Package Com.kobicc.cec.testDemo2;

Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;

public class IOTest2 {
public static void Main (string[] args) throws Exception {
String str = "Chinese";

//Mode one "use byte stream input, byte stream output. A byte buffer was used "
FileOutputStream fos = new FileOutputStream ("D:\\1.txt");
Fos.write (Str.getbytes ("UTF-8"));
Fos.close ();

FileInputStream FR = new FileInputStream ("D:\\1.txt");
byte[] buf = new byte[1024];
int Len;
String mystr = "";
while (len = Fr.read (BUF))! =-1) {
MyStr + = new String (buf, 0, Len, "UTF-8");
}
String mystr = new String (buf, 0, Len, "UTF-8"); Way Two
System.out.println (MYSTR);

//Mode two "use character stream input, character stream output. A character buffer was used "
FileWriter FW = new FileWriter ("D:\\2.txt");
Fw.write (str);
Fw.close ();

FileReader FR2 = new FileReader ("D:\\2.txt");
char[] Buf2 = new char[1024];
int len2 = Fr2.read (BUF2);
String myStr2 = new String (buf2, 0, len2);
System.out.println (MYSTR2);

//Way three "using character stream input, character stream output, no buffer used, direct read line"
PrintWriter pw = new PrintWriter ("D:\\3.txt", "utf-8");
Pw.write (str);
Pw.close ();

BufferedReader br = new BufferedReader (New InputStreamReader (
New FileInputStream ("D:\\3.txt"), "Utf-8");
String MYSTR3 = Br.readline ();
Br.close ();
System.out.println (MYSTR3);
}
}

Byte stream and character stream of Java IO and its difference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.