1, keyboard input 5 Student information (name, language score, math results, English scores), according to the total score from high to low deposit text files.
Code:
public class Treesetdemo {public static void main (string[] args) throws ioexception{//Create TreeSet object, using the interface anonymous inner class Implement Comparator interface treeset<student> ts=new treeset<student> (New comparator<student> () {// Override the Compare () method in the comparator interface @Override public int compare (Student s1,student s2) {//main sort Bar Pieces: Total, press from high to low output int num1=s2.sum (s2)-s1.sum (S1); Secondary sorting criteria, when the total grade is the same, the int num2= (num1==0) is compared by the student name content? S2.getname (). Length ()-s1.getname (). Length (): NUM1; return num2; } }); Keyboard entry Student Information System.out.println ("Please enter student information:"); for (int x=1;x<6;x++) {Scanner sc=new Scanner (system.in); System.out.print ("Please enter the name of" +x+ "Student:"); String Name=sc.nextline (); System.out.print ("Please enter the" +x+ "Student's language Score:"); int Chinesescore=sc.nextint (); System.out.print ("Please enter the math score of" +x+ "Student:"); int Mathscore=sc.nextint(); System.out.print ("Please enter the" +x+ "Student's English Score:"); int Englishscore=sc.nextint (); The input student information is encapsulated in the student object Student s=new Student (); S.setname (name); S.setchinesescore (Chinesescore); S.setmathscore (Mathscore); S.setenglishscore (Englishscore); Add the Student object to the collection Ts.add (s); }//create character buffer output stream object BufferedWriter bw=new bufferedwriter (New FileWriter ("18-1.txt")); Traverse for (Student s:ts) {//Take advantage of the APPEND function in StringBuffer to assemble the information that needs to be output StringBuffer sb=new Stringbuffe R (); Sb.append (S.getname ()). Append (","). Append (S.getchinesescore ()). Append (","). Append (S.getmathscore ()). Append ("," ). Append (S.getenglishscore ()). Append (","). Append (S.sum (s)); Writes information to a text file in Bw.write (Sb.tostring ()); NewLine Bw.newline (); Refresh Stream Bw.flush (); }//Close stream, Release resource Bw.close (); }}
Console:
Text file:
2, the known s.txt file has such a string: "HCEXFGIJKAMDNOQRZSTUVWYBPL"
Please write the program to read the data content, sorting the data after writing to Ss.txt.
public class Paixv { public static void main(String[] args) throws IOException { // 读取该文件的内容,存储到一个字符串中 BufferedReader br = new BufferedReader(new FileReader("E:\\西部开源\\作业\\课后练习\\java\\18-2.txt")); String line = br.readLine(); br.close(); // 把字符串转为字符数组 char[] chs = line.toCharArray(); // 对字符数组进行排序 Arrays.sort(chs); // 把排序后的字符数组转换为字符串 String s = new String(chs); // 把字符串再次写入ss.txt中 BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\西部开源\\作业\\课后练习\\java\\18-2W.txt")); bw.write(s); bw.newLine(); // 释放资源 bw.close(); }}
Java Collection/array sorting knowledge and IO-stream combination Example