0. Character stream and byte stream
1. Byte buffer stream
1.1 byte buffer stream construction method
1. Introduction to byte buffer stream
- lBufferOutputStream: this class implements buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without causing a call to the underlying system for each byte written
- lBufferedInputStream: creating a BufferedInputStream will create an internal buffer array. When bytes are read or skipped from the stream, the internal buffer will be refilled from the included input stream as needed, many bytes at a time
2. Construction method
Method name | explain |
---|
BufferedOutputStream(OutputStream out) | Create byte buffered output stream object |
BufferedInputStream(InputStream in) | Create byte buffered input stream object |
3. Example code
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//Byte buffered output stream: BufferedOutputStream(OutputStream out)
BufferedOutputStream bos = new BufferedOutputStream(new
FileOutputStream("myByteStream\\bos.txt"));
//Write data
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
//Release resources
bos.close();
//Byte buffered input stream: BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myByteStream\\bos.txt"));
//Read one byte of data at a time
// int by;
// while ((by=bis.read())!=-1) {
// System.out.print((char)by);
// }
//Read one byte array data at a time
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1) {
System.out.print(new String(bys,0,len));
}
//Release resources
bis.close();
}
}
2. Character stream
2.1 why does character stream appear
1. Introduction of character stream
- Because the byte stream is not particularly convenient to operate Chinese, Java provides character stream
- Character stream = byte stream + encoding table
2. Chinese byte storage mode
- When copying a text file with byte stream, the text file will also have Chinese, but there is no problem because the underlying operation will automatically splice bytes
Into Chinese, how to identify Chinese? - When storing Chinese characters, the first byte is negative no matter which encoding is selected for storage
2.2 coding table
1. What is a character set
- Character set is a set of all characters supported by the system, including national characters, punctuation marks, graphic symbols, numbers, etc
- To accurately store and recognize various character set symbols, the computer needs character coding. A set of character set must have at least one set of character coding.
- Common character sets include ASCII character set, GBXXX character set, Unicode character set, etc
2. Common character sets
1. ASCII character set
- ASCII: it is a set of computer coding system based on Latin alphabet, which is used to display modern English, mainly including control characters (enter key, backspace
Line feed key, etc.) and displayable characters (English uppercase and lowercase characters, Arabic numerals and Western symbols) - Basic ASCII character set, using 7 bits to represent a character, a total of 128 characters. The ASCII extended character set uses 8 bits to represent a character, a total of 256 characters, which is convenient to support common European characters. It is a collection of all characters supported by the system, including national characters, punctuation marks, graphic symbols, numbers, etc
2. GBXXX character set:
- GBK: the most commonly used Chinese code table. It is an extended specification based on GB2312 standard. It uses a double byte coding scheme and contains 21003 Chinese characters. It is fully compatible with GB2312 standard and supports traditional Chinese characters, Japanese and Korean characters
3. Unicode character set:
- UTF-8 encoding: it can be used to represent any character in Unicode standard. It is the preferred encoding in e-mail, web pages and other applications for storing or transmitting text. The Internet Engineering Task Force (IETF) requires that all Internet protocols must support UTF-8 coding. It uses one to four bytes to encode each character
- Coding rules:
- 128 US-ASCII characters, requiring only one byte encoding
- Latin and other characters require two byte encoding
- Most common words (including Chinese) are encoded in three bytes
- Other rarely used Unicode auxiliary characters use four byte encoding
2.3 encoding and decoding problems in string
1. Relevant methods
Method name | explain |
---|
byte[] getBytes() | Encode the String into a series of bytes using the platform's default character set |
byte[] getBytes(String charsetName) | Encodes the String into a series of bytes using the specified character set |
String(byte[] bytes) | Decodes the specified byte array using the platform's default character set to create a string |
String(byte[] bytes, String charsetName) | Creates a string by decoding the specified byte array from the specified character set |
2. Code demonstration
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Demo {
public static void main(String[] args) throws UnsupportedEncodingException {
//Define a string
String s = "China";
//byte[] bys = s.getBytes(); //[-28, -72, -83, -27, -101, -67]
//byte[] bys = s.getBytes("UTF-8"); //[-28, -72, -83, -27, -101, -67]
byte[] bys = s.getBytes("GBK"); //[-42, -48, -71, -6]
System.out.println(Arrays.toString(bys));
//String ss = new String(bys);
//String ss = new String(bys,"UTF-8");
String ss = new String(bys,"GBK");
System.out.println(ss);
}
}
2.4 encoding and decoding problems in character stream
1. Two classes related to encoding and decoding problems in character stream
- InputStreamReader: a bridge from byte stream to character stream
It reads bytes and decodes them into characters using the specified encoding
The character set it uses can be specified by name or explicitly, or it can accept the default character set of the platform - OutputStreamWriter: a bridge from character stream to byte stream
It is a bridge from character stream to byte stream. It uses the specified encoding to encode the written characters into bytes
The character set it uses can be specified by name or explicitly, or it can accept the default character set of the platform
2. Construction method
Method name | explain |
---|
InputStreamReader(InputStream in) | Create an InputStreamReader object using the default character encoding |
InputStreamReader(InputStream in,Stringchatset) | Creates an InputStreamReader object using the specified character encoding |
OutputStreamWriter(OutputStream out) | Creates an OutputStreamWriter object using the default character encoding |
OutputStreamWriter(OutputStream out,Stringcharset) | Creates an OutputStreamWriter object using the specified character encoding |
3. Code demonstration
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("myCharStream\\osw.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("myCharStream\\osw.txt"),"GBK");
osw.write("China");
osw.close();
//InputStreamReader isr = new InputStreamReader(new
FileInputStream("myCharStream\\osw.txt"));
InputStreamReader isr = new InputStreamReader(new
FileInputStream("myCharStream\\osw.txt"),"GBK");
//Read data one character at a time
int ch;
while ((ch=isr.read())!=-1) {
System.out.print((char)ch);
}
isr.close();
}
}
2.5 five ways of writing data in character stream
1. Method introduction
public class ConversionStreamDemo {
Method name | explain |
---|
void write(int c) | Write a character |
void write(char[] cbuf) | Write a character array |
void write(char[] cbuf, int off, int len) | Write part of a character array |
void write(String str) | Write a string |
void write(String str, int off, int len) | Write part of a string |
2. Refresh and close methods
Method name | explain |
---|
flush() | Refresh the stream, and then continue to write data |
close() | Close the flow and release resources, but brush a new flow before closing. Once closed, no more data can be written |
3. Code demonstration
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Demo {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("myCharStream\\osw.txt"));
//void write(int c): write a character
// osw.write(97);
// osw.write(98);
// osw.write(99);
//Void write (char [] cbuf): write a character array
char[] chs = {'a', 'b', 'c', 'd', 'e'};
// osw.write(chs);
//void write(char[] cbuf, int off, int len): write a part of the character array
// osw.write(chs, 0, chs.length);
// osw.write(chs, 1, 3);
//void write(String str): write a string
// osw.write("abcde");
//void write(String str, int off, int len): write a part of a string
// osw.write("abcde", 0, "abcde".length());
osw.write("abcde", 1, 3);
//Release resources
osw.close();
}
}
2.6 two ways of reading data from character stream
1. Method introduction
Method name | explain |
---|
int read() | Read data one character at a time |
int read(char[] cbuf) | Read one character array data at a time |
2. Code demonstration
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new
FileInputStream("myCharStream\\ConversionStreamDemo.java"));
//int read(): read data one character at a time
// int ch;
// while ((ch=isr.read())!=-1) {
// System.out.print((char)ch);
// }
//int read(char[] cbuf): read one character array data at a time
char[] chs = new char[1024];
int len;
while ((len = isr.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
//Release resources
isr.close();
}
}
2.7 character buffer stream
1. Introduction to character buffer stream
- BufferedWriter: writes text to the character output stream and buffers characters to provide efficient writing of single characters, arrays and strings. You can specify the buffer size or accept the default size. The default value is large enough for most purposes
- BufferedReader: reads text from the character input stream and buffers characters to provide efficient reading of characters, arrays and rows. You can specify the buffer size or use the default size. The default value is large enough for most purposes
2. Construction method
Method name | explain |
---|
BufferedWriter(Writer out) | Create character buffered output stream object |
BufferedReader(Reader in) | Create character buffered input stream object |
3. Code demonstration
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//BufferedWriter(Writer out)
BufferedWriter bw = new BufferedWriter(new
FileWriter("myCharStream\\bw.txt"));
bw.write("hello\r\n");
bw.write("world\r\n");
bw.close();
//BufferedReader(Reader in)
BufferedReader br = new BufferedReader(new
FileReader("myCharStream\\bw.txt"));
//Read data one character at a time
// int ch;
// while ((ch=br.read())!=-1) {
// System.out.print((char)ch);
// }
//Read one character array data at a time
char[] chs = new char[1024];
int len;
while ((len=br.read(chs))!=-1) {
System.out.print(new String(chs,0,len));
}
//Release resources
br.close();
}
}
2.8 special functions of character buffer stream
1. Method introduction
Method name | explain |
---|
void newLine() | Write a line separator. The line separator string is defined by the system property |
Method name | explain |
---|
String readLine() | Read a line of text. The result is a string containing the contents of the line, excluding any line termination characters. If the end of the stream has reached, it is null |
2. Code demonstration
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//Create character buffered output stream
BufferedWriter bw = new BufferedWriter(new
FileWriter("myCharStream\\bw.txt"));
//Write data
for (int i = 0; i < 10; i++) {
bw.write("hello" + i);
//bw.write("\r\n");
bw.newLine();
bw.flush();
}
//Release resources
bw.close();
//Create character buffered input stream
BufferedReader br = new BufferedReader(new
FileReader("myCharStream\\bw.txt"));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
//Release resources
br.close();
}
}