This is the 12th time for me to participate in the August More Text Challenge. For details, see:August is more challenging
Introduction to byte buffering streams
-
BufferOutputStream: This class implements buffering output streams. 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
-
BufferedInputStream: Creating a BufferedInputStream creates an array of internal buffers. When bytes are read or skipped from the stream, the internal buffer is refilled as needed from the contained input stream, multiple at a time
A constructor
The method name | instructions |
---|---|
BufferedOutputStream(OutputStream out) | Creates a byte buffer output stream object |
BufferedInputStream(InputStream in) | Creates a byte buffer input stream object |
public class BufferStreamLearn1 {
public static void main(String[] args) throws IOException {
// BufferedOutputStream(OutputStream out)
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("File\shixf.txt"));
/ / write data
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
// Release resources
bos.close();
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("File\shixf.txt"));
// Read data one byte at a time
/*int by; while ((by=bis.read())! =-1) { System.out.print((char)by); } * /
// Read the array data one byte 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 resourcesbis.close(); }}Copy the code
The output file
Console output
Characters of the flow
Character Stream Introduction
-
An introduction to character streams
Since byte streams are not particularly convenient to operate on Chinese, Java provides character streams
Character stream = byte stream + encoding table
-
Chinese byte storage
When you copy a text file using a byte stream, the text file will also have Chinese characters, but there is no problem because eventually the underlying operation will automatically concatenate the bytes into Chinese characters. How to identify the Chinese characters?
When storing Chinese characters, no matter which encoding is selected, the first byte is always negative
Code table
- What is a character set
A collection of all characters supported by a system, including national characters, punctuation marks, graphic symbols, numbers, etc
L To accurately store and recognize the symbols of various character sets, the computer needs to carry out character encoding, a set of character set must be at least one set of character encoding. Common character sets include ASCII character set, GBXXX character set, Unicode character set and so on
-
Common character set
-
ASCII character set:
LASCII: A computer coding system based on the Latin alphabet for displaying modern English, consisting mainly of control characters (return keys, backspace, newline keys, etc.) and displayable characters (upper and lower case characters, Arabic numerals, and Western symbols)
The basic ASCII character set, which uses 7 bits to represent one character, has 128 characters in total. ASCII’s extended character set uses 8 bits to represent one character, a total of 256 characters, to facilitate support for common European characters. A collection of all characters supported by a system, including national characters, punctuation marks, graphic symbols, numbers, etc
-
GBXXX character set:
GBK: the most commonly used Chinese code table. GB2312 standard is based on the extended specification, the use of double byte coding scheme, a total of 21003 Chinese characters, fully compatible with GB2312 standard, while supporting traditional Chinese characters and Japanese and Korean Characters
-
Unicode Character set:
Utf-8 encoding: it can be used to represent any character in the Unicode standard and is the preferred encoding for E-mail, web pages, and other applications that store or transmit text. The Internet Engineering Working Group (IETF) requires that all Internet protocols must support UTF-8 encoding. It uses one to four bytes to encode each character
Coding rules:
128 US-ASCII characters, only one byte encoding
Characters such as Latin, which require two bytes of encoding
Most common characters (including Chinese) are encoded in three bytes
Other rarely used Unicode auxiliary characters, using a four-byte encoding
-
Encoding/decoding problem in string
The method name | instructions |
---|---|
byte[] getBytes() | The String is encoded as a sequence of bytes using the platform’s default character set |
byte[] getBytes(String charsetName) | Encodes the String as a sequence of bytes using the specified character set |
String(byte[] bytes) | Create a string by decoding the specified byte array using the platform’s default character set |
String(byte[] bytes, String charsetName) | Creates a string by decoding a specified array of bytes from the specified character set |
public class StringLearn {
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); }}Copy the code
Encoding and decoding in a character stream
-
InputStreamReader: 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, can be explicitly specified, or can accept the platform’s default character set
-
OutputStreamWriter: bridge from character stream to byte stream
Is a bridge from a character stream to a byte stream that encodes written characters into bytes using the specified encoding
The character set it uses can be specified by name, can be explicitly specified, or can accept the platform’s default character set
The method name | instructions |
---|---|
InputStreamReader(InputStream in) | Create an InputStreamReader object using the default character encoding |
InputStreamReader(InputStream in,String chatset) | Creates an InputStreamReader object using the specified character encoding |
OutputStreamWriter(OutputStream out) | Create an OutputStreamWriter object using the default character encoding |
OutputStreamWriter(OutputStream out,String charset) | Creates an OutputStreamWriter object with the specified character encoding |
public class ConversionStreamLearn { public static void main(String[] args) throws IOException { //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("myCharStream\osw.txt")); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("File\shixf.txt"),"GBK"); Osw. Write (" China "); osw.close(); //InputStreamReader isr = new InputStreamReader(new FileInputStream("myCharStream\osw.txt")); try (InputStreamReader isr = new InputStreamReader(new FileInputStream("File\shixf.txt"), "GBK") {// read one character at a time; while ((ch = isr.read()) ! = -1) { System.out.print((char) ch); } isr.close(); }}}Copy the code
The output file
Console output
Five ways for a character stream to write data
The method name | instructions |
---|---|
void write(int c) | Write a character |
void write(char[] cbuf) | Write an array of characters |
void write(char[] cbuf, int off, int len) | Writes 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 |
The method name | instructions |
---|---|
flush() | Refresh the stream, after which you can continue to write data |
close() | The stream is closed, freeing resources, but the stream is refreshed before closing. Once closed, no data can be written |
public class OutputStreamWriterLearn {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("File\shixf.txt"));
//void write(int c) : write a character
// osw.write(97);
// osw.write(98);
// osw.write(99);
//void writ(char[] cbuf) : writes a character array
char[] chs = {'a'.'b'.'c'.'d'.'e'};
// osw.write(chs);
//void write(char[] cbuf, int off, int len) : write 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)
// osw.write("abcde", 0, "abcde".length());
osw.write("abcde".1.3);
// Release resourcesosw.close(); }}Copy the code
There are two ways for a character stream to read data
The method name | instructions |
---|---|
int read() | Read data one character at a time |
int read(char[] cbuf) | Read the character array data one at a time |
public class InputStreamReaderLearn {
public static void main(String[] args) throws IOException {
// shixf.txt -> {b,c,d}
InputStreamReader isr = new InputStreamReader(new FileInputStream("File\shixf.txt"));
//int read() : reads 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 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 resourcesisr.close(); }}Copy the code
Console output
Character buffer stream
Introduction to character buffering streams
-
BufferedWriter: Writes text to a character output stream, buffering characters to provide efficient writing of single characters, arrays, and strings. The buffer size can be specified, or the default size can be accepted. The default is large enough to be used for most purposes
-
BufferedReader: Reads text from the character input stream, buffering characters to provide efficient reading of characters, arrays and lines. The buffer size can be specified, or the default size can be used. The default is large enough to be used for most purposes
The method name | instructions |
---|---|
BufferedWriter(Writer out) | Creates a character buffer output stream object |
BufferedReader(Reader in) | Creates a character buffer input stream object |
public class BufferedStreamLearn {
public static void main(String[] args) throws IOException {
//BufferedWriter(Writer out)
BufferedWriter bw = new BufferedWriter(new FileWriter("File\ShixfBW.txt"));
bw.write("hello\r\n");
bw.write("world\r\n");
bw.close();
//BufferedReader(Reader in)
BufferedReader br = new BufferedReader(new FileReader("File\ShixfBW.txt"));
// Read data one character at a time
// int ch;
// while ((ch=br.read())! = 1) {
// System.out.print((char)ch);
/ /}
// Read one character array at a time
char[] chs = new char[1024];
int len;
while((len=br.read(chs))! = -1) {
System.out.print(new String(chs,0,len)); } br.close(); }}Copy the code
The output file
Console output
Character buffering stream features
BufferedWriter
The method name | instructions |
---|---|
void newLine() | Writes a line delimiter string defined by a system property |
BufferedReader:
The method name | instructions |
---|---|
String readLine() | Read a line of text. The result is a string containing the contents of the line, excluding any line terminator characters, or null if the end of the stream has already been reached |
public class BufferedStreamLearn {
public static void main(String[] args) throws IOException {
// Create a character buffer output stream
BufferedWriter bw = new BufferedWriter(new FileWriter("File\ShixfBW.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 a character buffered input stream
BufferedReader br = new BufferedReader(new FileReader("File\ShixfBW.txt"));
String line;
while((line=br.readLine())! =null) { System.out.println(line); } br.close(); }}Copy the code
The output