the reason and thought of character stream appearing
? Because the byte stream operation Chinese is not particularly convenient, so Java provides a character stream. Character stream = byte stream + encoding table.
Coding Table Overview and common coding tables
█
Encoding Table
? A table consisting of characters and their corresponding values
█
Common Coding Tables
? Ascii/unicode Character set? Iso-8859-1 Latin-1? Gb2312/gbk/gb18030? BIG5? UTF-8
public class Testmain {/* * getBytes (String charsetname); encodes this String into a byte sequence using the specified character set and stores the result in a new byte array. string (byte[] bytes, string charsetname); constructs a new String by decoding the specified byte array with the specified charset. */public static void Main (string[] args) {string string = ' Hello World ';byte[] bytes = String.getbytes ();//Codingfor (int i=0;i<bytes.length;i++) {System.out.print (bytes[i]);//Output byte code}//decodingstring jiemastrin = new string (bytes);//Constructs a new String by decoding the specified byte array using the platform's default character set. //This platform is the operating system, the default encoding GBKSystem.out.print (' \ n ' +jiemastrin); } } |
public static void Main (string[] args) throws Unsupportedencodingexception {string string = ' Hello World ' Mei Hao ";//Codingbyte[] bytes = string.getbytes ("Utf-8"); //parameter does not write is using the system default GBK, but I was changed to Utf-8;for (int i=0;i<bytes.length;i++) {System.out.print (bytes[i]);//Output byte code}//decodingString Jiemastrin = new string (bytes);//Constructs a new String by decoding the specified byte array using the platform's default character set. (I've changed the default to Utf-8 here.)string jiemastrin = new String (bytes, "Utf-8"); //This platform is the operating system, the default encoding GBKSystem.out.print (' \ n ' +jiemastrin); } } |
//unicode Code: Java Virtual machine internal use
String str = "Mei Hao"; The stored string constant area is stored in Unicode code.
character Stream overview (conversion stream)
█
outputstreamwriter character output stream
? Public OutputStreamWriter (OutputStream out); //Create a outputstreamwriter using the default character encoding. ? Public outputstreamwriter (OutputStream out,string charsetname);
█
InputStreamReader character input stream
? Public InputStreamReader (InputStream in); //Create a inputstreamreader that uses the default character set. ? Public InputStreamReader (InputStream in,string charsetname); //Create a user with the specified character set
Java--io Class Character Stream overview