This is a question (changed) in the video tutorial on Java employment training by Mr. Zhang Xiaoxiang ): Compile the following program code to analyze and observe the running results of the program:Import java. Io .*; Public class testcodeio { Public static void main (string [] ARGs) throws exception { Inputstreamreader ISR = new inputstreamreader (system. In, "iso8859-1 "); Bufferedreader BR = new bufferedreader (ISR ); String strline = Br. Readline (); BR. Close (); ISR. Close (); System. Out. println (strline ); } } After running the program, enter "China" and the output result is ??? Úo Modify the above program in the following two ways: 1. Modify statements in a program Inputstreamreader ISR = new inputstreamreader (system. In, "iso8859-1 "); 2. Modify the following statement without modifying the preceding statement. System. Out. println (strline );
The first method is very simple. You only need to change it to the following method. We will not discuss it in detail here. Inputstreamreader ISR = new inputstreamreader (system. In, "gb2312 ");
Here I want to discuss in detail how to change the second method. At first I changed it like this. System. Out. println (new string (strline. getbytes (), "iso8859-1 ")); The output result after "China" is input is not garbled as described above, but garbled. Obviously, this method is incorrect! I would like to thank the software migrant workers for telling me the correct correction. System. Out. println (new string (strline. getbytes ("iso8859-1 ")));
What are the differences between the two methods? To facilitate reading, I will post correct and incorrect methods: Import java. Io .*; Public class testcodeio { Public static void main (string [] ARGs) throws exception { Inputstreamreader ISR = new inputstreamreader (system. In, "iso8859-1 "); // Create an inputstreamreader that uses the given charset Decoder Bufferedreader BR = new bufferedreader (ISR ); String strline = Br. Readline (); BR. Close (); ISR. Close (); System. Out. println (strline ); System. Out. println (new string (strline. getbytes (), "iso8859-1"); // Error Correction // Encodes this string (strline) into a sequence of bytes using the platform's // Default charset (gb2312) then constructs a new string by decoding // Specified array of bytes using the specified charset (iso8859-1) // Because this string (strline) uses the charset decoder "iso8859-1", so it can // Only be encoded by "iso8859-1", cann't be encoded by the platform's default // Charset "gb2312", so this line is wrong. System. Out. println (new string (strline. getbytes ("iso8859-1"); // correct // Encodes this string (strline) into a sequence of bytes using the named // Charset (iso8859-1), then constructs a new string by decoding // Specified array of bytes using the platform's default charset (gb2312 ). // This line is right. } }
The above English comments have already been made clear. I 'd like to explain them here: The first is the incorrect correction method system. Out. println (new string (strline. getbytes (), "iso8859-1 ")); This Code uses the default encoding method (gb2312) for strings in strline) Converts to a byte sequence and constructs a new String object and print it to the screen. Where is the error? Please note that this Code Section Inputstreamreader ISR = new inputstreamreader (system. In, "iso8859-1 "); Bufferedreader BR = new bufferedreader (ISR ); String strline = Br. Readline (); Here the content stored in strline is stored in the specified encoding method (iso8859-1), and converted to bytecode (This code strline. getbytes () uses the default gb2312 encoding. Garbled output! Then we use the gb2312 encoded byte sequence to construct a new String object. Iso8859-1 encoding, so the output garbled code is different from system. Out. println (strline. As for the correct method, it does not need to be described in detail, first of all, the strline with the iso8859-1 encoding to convert the byte And then use the system's default encoding method (gb2312) to construct a new String object and print the output. |