标签:
import java.io.*; class EncodeStream { public static void main(String[] args)throws IOException { //writeText(); readText(); } public static void writeText()throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("GBK.txt"));//默认是GBK编码 //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("你好!"); osw.flush(); osw.close(); } public static void readText()throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("GBK.txt"),"GBK"); char[] buf = new char[1024]; int len = 0; while((len = isr.read(buf))!=-1) { String s = new String(buf,0,len); System.out.println(s); } String str = isr.getEncoding(); System.out.println(str); isr.close(); } }
import java.util.*; class EncodeDemo { public static void main(String[] args)throws Exception { String s = "你好"; //编码 byte[] b1 = s.getBytes("GBK");//[-60, -29, -70, -61]四个字节 //byte[] b1 = s.getBytes("UTF-8");//[-28, -67, -96, -27, -91, -67]六个字节 //byte[] b1 = s.getBytes("Unicode");//[-2, -1, 79, 96, 89, 125] //byte[] b1 = s.getBytes("ISO8859-1");//[63, 63]两个字节 //System.out.println(Arrays.toString(b1)); //解码 String str1 = new String(b1,"ISO8859-1");//应该是GBK的编码,结果解成了ISO8859-1编码,此时会出现乱码,需要重新编码解码 System.out.println(str1);//发现乱码结果是???? //对乱码再一次编码 byte[] b2 = str1.getBytes("ISO8859-1"); System.out.println(Arrays.toString(b2));//[-60, -29, -70, -61] //再一次解码 String str2 = new String(b2); System.out.println(str2);//你好 } }
class EncodeDemo2 { public static void main(String[] args)throws Exception { String s = "联通";//这两个字出现GDK编码和UTF-8编码重复的现象 byte[] by = s.getBytes("gbk"); for(byte b: by) { System.out.println(Integer.toBinaryString(b&255)); } } } /* 11000001 10101010 11001101 10101000 以上复合UTF-8的编码 */
import java.io.*; import java.util.*; class Student implements Comparable<Student> { private String name; private int math; private int English; private int Chinese; private int ScoreSum; Student(String name,int math,int English,int Chinese) { this.name = name; this.math = math; this.English = English; this.Chinese = Chinese; ScoreSum = math+English+Chinese; } public String getname() { return name; } public int getScoreSum() { return ScoreSum; } public int hashCode() { return name.hashCode()+ScoreSum*78; } public boolean equals(Object obj) { if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象!"); Student s = (Student)obj; return this.name.equals(s.name) && this.ScoreSum==s.ScoreSum; } public int compareTo(Student s) { int num = new Integer(this.ScoreSum).compareTo(new Integer(s.ScoreSum)); return num==0? this.name.compareTo(s.name):num; } public String toString() { return "Student["+name+","+math+","+English+","+Chinese+"]"; } } class StudentInfoTool { public static Set<Student> getStudent()throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); Set<Student> stus = new TreeSet<Student>(new Mycomparator());//也可以采用逆转比较器的方法 String line = null; while((line = bufr.readLine())!=null) { if("over".equals(line)) break; String[] info = line.split(","); Student stu = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3])); stus.add(stu); } bufr.close(); return stus; } public static void WriteInfo(Set<Student> stus)throws IOException { BufferedWriter bufw = new BufferedWriter(new FileWriter("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(); } } class StudentFileTest { public static void main(String[] args)throws IOException { Set<Student> set = StudentInfoTool.getStudent(); StudentInfoTool.WriteInfo(set); } } class Mycomparator implements Comparator<Student> { public int compare(Student s1,Student s2) { if(s1.getScoreSum()>s2.getScoreSum()) return -1; if(s1.getScoreSum()<s2.getScoreSum()) return 1; else return 0; } } /* 输入数据: zhangsan,78,89,87 lisi,89,86,78 wangwu,78,56,68 zhaoliu,89,99,100 zhouqi,45,68,97 打印结果: Student[zhaoliu,89,99,100] 288 Student[zhangsan,78,89,87] 254 Student[lisi,89,86,78] 253 Student[zhouqi,45,68,97] 210 Student[wangwu,78,56,68] 202 */
标签:
原文地址:http://www.cnblogs.com/XYQ-208910/p/4918797.html