character stream
A Chinese character is stored, GBK encoding takes up 2 bytes, and UTF-8 encoding takes up 3 bytes.
Character stream = byte stream + encoding table;
when Chinese characters are stored, no matter which encoding is selected for storage, the first byte Both are negative,
only text data can be copied
coding
Store the characters in the computer according to certain rules
Decoding: parse and display the binary numbers in the computer according to the corresponding rules.
Character encoding: the correspondence between a set of natural language characters and binary numbers
Common character sets are: ASCII set: a computer encoding system based on the Latin alphabet, used to display modern English, is a set of all characters supported by the system
GBXXX series character sets: such as GB2312: two characters greater than 127 are combined to represent a Chinese character;
GBK : the most commonly used Chinese code table, which contains more than 2W Chinese characters, fully compatible with GB2312;
GB18030: the latest Chinese code table, More than 70,000 Chinese characters are included, and minority languages are supported.
Unicode character set: Designed to express any character in any language, it is a standard in the industry, also known as Unicode and standard universal code. There are three encoding schemes: UTF-8, UTF-16, UTF-32; UTF-8
encoding : Can be used to represent any character in unicode, using 1-4 bytes to encode each character. The Internet Work Engineering Group requires that all Internet protocols support UTF-8 encoding.
Encoding and decoding problems in strings
Encoding method:
byte[] getBytes() (use the default encoding)
byte[] getBytes(String charsetName)
uses the named character set to encode the String into a series of bytes, and store the result in a new byte array.
Decoding method:
String(byte[] bytes) (use default decoding)
String(byte[] bytes, Charset charset)
Construct a new String to decode charset by specifying an array of specified bytes
Code example:
//编码
String s = "哈哈";
byte[] byt = s.getBytes("GBK");
System.out.println(Arrays.toString(byt));
//解码
String ss = new String(byt,"GBK");
System.out.println(ss);
Decoding and encoding in character streams
Abstract base class of character stream:
Reader: abstract base class of character input stream
Writer: abstract base class of character output stream
Two classes related to encoding and decoding in character stream:
InputStringReader
OutputStringWriter
Example:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("1215/file.txt"),"UTF-8");
osw.write("哈哈");
osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("1215/file.txt"),"UTF-8");
int ch;
while ((ch=isr.read()) != -1){
System.out.println((char)ch);
}
isr.close();
5 ways to write data in character stream
After the character stream writes data, it is temporarily stored in the buffer area. If it is to be displayed before closing the output stream, it must be written to the file with the flush method. After flushing, it can continue to write
Two methods of character stream reading data
The format is the same as the byte stream, except that one is a byte and the other is a character
char[] cha = new char[1024];
int len;
while ((len = isr.read(cha)) != -1){
System.out.println(new String(cha,0,len));
}
isr.close();
FileWriter,FileReader
Convenient class to write character files. The constructor for this class assumes that the default character encoding and default byte buffer size are acceptable. To specify these values by yourself, if encoding issues are involved, please construct an OutputStreamWriter on FileOutputStream.
FileReader, FileWriter(String fileName) Convenience class for writing character files,
constructs a FileWriter object given a filename.
FileReader fr = new FileReader("1215/file.txt");
FileWriter fw = new FileWriter("1215/file1.txt");
char[] cha = new char[1024];
int ch;
while((ch=fr.read()) != -1){
fw.write(cha,0,ch);
}
fw.close();
fr.close();
character buffer stream
A BufferedReader
reads text from a character input stream, buffering the characters to provide efficient reading of characters, arrays, and lines.
A BufferedWriter
writes text to a character output stream, buffering characters to provide efficient writing of individual characters, arrays, and strings.
The buffer size can be specified, or the default size can be used
BufferedWriter bw = new BufferedWriter(new FileWriter("1215/file.txt"));
bw.write("yoyo");
bw.write("sosos");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));
int len;
char[] cha =new char[1024];
while((len=br.read(cha)) != -1){
System.out.println(new String(cha,0,len));
}
while((len=br.read()) != -1){
System.out.print((char)len);
}
br.close();
Features specific to character buffer streams:
//按整行读取(只读内容,不读换行的符号)
String line;
while ((line=br.readLine()) != null){
System.out.println(line);
}
copy files
String line;
while ((line=br.readLine()) != null){
bw.write(line);
bw.newLine();
}
code example
file to collection
BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));
ArrayList<String> list = new ArrayList<>();
String line;
while((line=br.readLine())!= null){
list.add(line);
}
for(String s : list){
System.out.println(s);
}
Lottery Mini Program
BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));//file里按行写入名单
ArrayList<String> list = new ArrayList<>();
String line;
while((line=br.readLine())!= null){
list.add(line);
}
Random r = new Random();
int index = r.nextInt(list.size());
String luck = list.get(index);
System.out.println("the luck one is "+ luck);
br.close();