ASCII: U.S. Information Standard Information code, denoted by a byte of 7. iso8859-1: Latin Code table European Code table, denoted by a byte of 8 bits. GB2312: China's Chinese code table. GBK: China's Chinese Code table upgrade, the integration of more Chinese character symbols. Unicode: International standard Code, fused with a variety of text. All text is represented in two bytes, and Java uses this encoding table. UTF-8: A maximum of three bytes to represent one character. ///Example 1:
ImportJava.io.*;classencodestream{ Public Static voidMain (string[] args)throwsIOException {//writetext ();ReadText (); } Public Static voidWRITETEXT ()throwsIOException {outputstreamwriter OSW=NewOutputStreamWriter (NewFileOutputStream ("GBK.txt"));//The default is GBK encoding//OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("Utf-8.txt"), "Utf-8"); //OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("Unicode.txt"), "Unicode"); //OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("ASCII.txt"), "ASCII"); //OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("Iso8859-1.txt"), "iso8859-1");Osw.write ("Hello!"); Osw.flush (); Osw.close (); } Public Static voidReadText ()throwsIOException {InputStreamReader ISR=NewInputStreamReader (NewFileInputStream ("GBK.txt"), "GBK"); Char[] buf =New Char[1024]; intLen = 0; while(len = Isr.read (buf))!=-1) {String s=NewString (buf,0, Len); System.out.println (s); } String str=isr.getencoding (); System.out.println (str); Isr.close (); }}encoding: String into byte arrayString------>byte[] str.getbytes ()/default gbk/or Str.getbytes (Charset Charset)/Set encoding mode/decoding: Byte array becomes stringbyte[]------>string new String (byte[])/default gbk/or new string (Byte[],charset Charset)/Set encoding mode////Example 2:
ImportJava.util.*;classencodedemo{ Public Static voidMain (string[] args)throwsException {String s= "Hello"; //Coding byte[] B1 = S.getbytes ("GBK");//[-60,-29,-70,-61] four bytes//byte[] B1 = s.getbytes ("UTF-8");//[-28,-67,-96,-27,-91,-67] Six bytes//byte[] B1 = s.getbytes ("Unicode");//[-2,-1 ,.//byte[] B1 = s.getbytes ("iso8859-1");//[63, 63] Two bytes//System.out.println (Arrays.tostring (B1)); //decodingString str1 =NewString (B1, "iso8859-1");//should be GBK code, the result of the solution to iso8859-1 encoding, this time will appear garbled, need to re-encode decodingSystem.out.println (STR1);//found garbled result is???? //encode the garbled code once again byte[] B2 = Str1.getbytes ("Iso8859-1"); System.out.println (arrays.tostring (B2));//[ -60, -29, -70, -61]//Decode once againString str2 =NewString (B2); System.out.println (STR2);//Hello }}A special coding phenomenon occurs when you write the word "Unicom" in the text .///Example 3:
classencodedemo2{ Public Static voidMain (string[] args)throwsException {String s= "Unicom";//These two words appear GDK encoding and UTF-8 coding repetition phenomenon byte[] by = S.getbytes ("GBK"); for(byteb:by) {System.out.println (integer.tobinarystring (b&255)); } }}/*11000001101010101100110110101000 + Coding of composite UTF-8*/
Comprehensive application of the file:Example 4:Exercise: There are 5 students, each student has 3 courses of results, from the keyboard input above data (including the name, the results of three classes), output format: such as: Zhangsan 30,40,60. The total score is calculated and the information of each student and the calculated scores are stored in the disk file "Stud.txt" in high and low order. steps:1. Describe the student object. 2. Define a tool class that can manipulate student objects. thought:1. Encapsulates a student object by obtaining a row of data entered from the keyboard and removing the data from that row. 2. Because there are many students, they need to be stored. Use the TreeSet collection because you want to sort the total number of students, so you can use the collections. 3. Write the information in the collection to a file.
ImportJava.io.*;ImportJava.util.*;classStudentImplementsComparable<student>{ PrivateString name; Private intMath; Private int中文版; Private intChinese; Private intscoresum; Student (String name,intMathint中文版,intChinese) { This. Name =name; This. Math =Math; This. 中文版 =中文版; This. Chinese =Chinese; Scoresum= math+english+Chinese; } PublicString getname () {returnname; } Public intgetscoresum () {returnscoresum; } Public inthashcode () {returnName.hashcode () +scoresum*78; } Public Booleanequals (Object obj) {if(! (objinstanceofStudent)) Throw NewRuntimeException ("Not student Object!")); Student s=(Student) obj; return This. Name.equals (S.name) && This. scoresum==s.scoresum; } Public intCompareTo (Student s) {intnum =NewInteger ( This. scoresum). CompareTo (NewInteger (s.scoresum)); returnNum==0? This. Name.compareto (s.name): num; } PublicString toString () {return"Student[" +name+ "," +math+ "," +english+ "," +chinese+ "]"; }}classstudentinfotool{ Public StaticSet<student> getstudent ()throwsIOException {BufferedReader bufr=NewBufferedReader (NewInputStreamReader (system.in)); Set<Student> Stus =NewTreeset<student> (NewMycomparator ());//You can also use the reverse comparator methodString Line=NULL; while(line = Bufr.readline ())! =NULL) { if("Over". Equals (line)) Break; String[] Info= Line.split (","); Student Stu=NewStudent (Info[0],integer.parseint (info[1]), Integer.parseint (Info[2]), Integer.parseint (info[3])); Stus.add (Stu); } bufr.close (); returnStus; } Public Static voidWriteinfo (set<student> stus)throwsIOException {bufferedwriter BUFW=NewBufferedWriter (NewFileWriter ("Stud.txt")); //bufferedwriter BUFW = new BufferedWriter (new OutputStreamWriter (System.out)); for(Student stu:stus) {bufw.write (stu.tostring ()+ "\ T"); Bufw.write (Stu.getscoresum ()+""); Bufw.newline (); Bufw.flush (); } bufw.close (); }}classstudentfiletest{ Public Static voidMain (string[] args)throwsIOException {Set<Student> set =studentinfotool.getstudent (); Studentinfotool.writeinfo (set); }}classMycomparatorImplementsComparator<student>{ Public intCompare (Student s1,student s2) {if(S1.getscoresum () >s2.getscoresum ())return-1; if(S1.getscoresum () <s2.getscoresum ())return1; Else return0; }}/*input data: zhangsan,78,89,87lisi,89,86,78wangwu,78,56,68zhaoliu,89,99,100zhouqi,45,68,97 print Result: Student[zhaoliu, 89,99,100] 288student[zhangsan,78,89,87] 254student[lisi,89,86,78] 253student[zhouqi,45,68,97] 210Student[wang WU,78,56,68] 202*/
Java: A detailed description of the code