JAVA 23 Character encoding issues

Source: Internet
Author: User

GBK of two bytes. Convert the stream. You can specify a code table when using a transform stream. InputStreamReader in = new InputStreamReader (New FileInputStream ("Abc.txt"), "Utf-8");// Read the text file in utf-8 form outstreamwrtier out = new Outstreamwriter (New Fileoutputstrem ("Abc.txt"), "utf-8") '//write to file in Utf-8 encoding. Encoding: String variable byte array decoding: Byte array variable string string-->byte[]:str.getbytes (charsername) byte-->string:new string (byte[], CharsetName) Example:Import Java.io.*;import java.util.*;p ublic class Test {  public static void main (string[] args) throws Ioexcept ion {  String s= "Hello";  byte [] B1 = S.getbytes ("GBK");  SOP (Arrays.tostring (B1));   string S1 = new String (B1, "GBK");  string s2 = new String (B1, "iso8859-1"); Not for Utf-8, because both Utf-8 and GBK support Chinese    sop (S1);  SOP (S2);   byte [] b2 = S2.getbytes ("iso8859-1"); nbsp String s3 = new String (B2, "GBK");  SOP (S3);  } public static void sop (Object o)  {  System.out.println (o);   }}   Comprehensive Exercise:/* There are five students, each student has 3 courses of results, from the keyboard input above data (including name, three lessons), input format: such as: Zhagnsan,30,40,60 calculated the total score, And the student's information and calculated total score high and low order in the disk file "Stud.txt".  1, describes the student object. 2, defines a tool class that can manipulate student objects.   thought: 1, by getting the keyboard to input a row of data, and the information in the row is taken out to encapsulate the student object. 2, because students have a lot, then you need to store, use to the collection. Because the student's total score is to be sorted.   So you can use TreeSet. 3, writes the information of the collection to a file.   */  import java.io.*;import java.util.*;p ublic class Test {  public static void main ( String[] args) throws Ioexception {  InputStream in =system.in;  bufferedreader bufr= new BufferedReader (New InputStreamReader (in));   treeset<student> ts = new treeset<student> ();  while (true)   {   string line = Bufr . ReadLine ();   if (Line.equals ("over"))     break;   else   {    string lines [] =line.split ("");    String a=lines[0];    int b = Integer.parseint (lines[1]);  & nbsp int c = Integer.parseint (lines[2]);    int d = integer.parseint (Lines[3]);    Ts.add (New Student (A, B, c,d));   } }   bufferedwriter fw = new BufferedWriter ("FileWriter". TXT "));  iterator<student> it = ts.iterator ();  while (It.hasnext ())   {   student st= It.next ();      fw.write (st.name);   fw.write ("");   fw.write ( Integer.tostring (St.yuwen));   fw.write ("");    Fw.write (Integer.tostring (St.shuxue));   fw.write ("");   fw.write (Integer.toString ( St.yingyu));   fw.write ("");   fw.write (integer.tostring (st.total));   fw.newLine () ;  }  fw.close ()    } public static void sop (Object o)  {  System.out.println (o);   }} class Student implements comparable{ string name; int Yuwen; int shuxue; int Yingyu; int total; student (String name,int yuwen,int shuxue,int Yingyu)  {  this.name=name;  this.yuwen=yuwen;  this.shuxue=shuxue;  this.yingyu=yingyu;  total=yuwen+shuxue+yingyu; }   public int CompareTo (Object stu)  {  Student st = (Student) stu;  return this.total-st.total;& nbsp;}}

JAVA 23 character encoding issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.