1. 程式人生 > >Java集合/數組排序知識與IO流結合實例

Java集合/數組排序知識與IO流結合實例

Java集合知識與IO流知識結合實例 選擇器排序 TreeSet集合 字符緩沖流 數組排序

1 、鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低存入文本文件。
代碼:

public class TreeSetDemo {
    public static void main(String[] args) throws  IOException{
        //創建TreeSet對象,用接口匿名內部類的方式實現Comparator接口
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
            //重寫Comparator接口中的compare()方法
            @Override
            public int compare(Student s1,Student s2) {
            //主要排序條件:總成績,按從高到低輸出
            int num1=s2.sum(s2)-s1.sum(s1);
            //次要排序條件,當總成績相同時按學生姓名內容比較
            int num2=(num1==0)?s2.getName().length()-s1.getName().length():num1;
               return num2;
            }       
        });
        //鍵盤錄入學生信息
        System.out.println("請輸入學生信息:");
        for(int x=1;x<6;x++) {
            Scanner sc=new Scanner(System.in);
            System.out.print("請輸入第"+x+"名學生的姓名:");
            String name=sc.nextLine();
            System.out.print("請輸入第"+x+"名學生的語文成績:");
            int chineseScore=sc.nextInt();
            System.out.print("請輸入第"+x+"名學生的數學成績:");
            int mathScore=sc.nextInt();
            System.out.print("請輸入第"+x+"名學生的英語成績:");
            int englishScore=sc.nextInt();
            //將錄入的學生信息封裝到學生對象裏
            Student s=new Student();
            s.setName(name);
            s.setChineseScore(chineseScore);
            s.setMathScore(mathScore);
            s.setEnglishScore(englishScore);
            //把學生對象添加到集合中
            ts.add(s);
        }

        //創建字符緩沖輸出流對象
        BufferedWriter bw=new BufferedWriter(new FileWriter("18-1.txt"));
        //遍歷
        for(Student s:ts) {
            //利用StringBuffer中的追加功能,將需要輸出的信息集合在一起
            StringBuffer sb=new StringBuffer();
            sb.append(s.getName()).append(",").append(s.getChineseScore()).append(",").append(s.getMathScore())
            .append(",").append(s.getEnglishScore()).append(",").append(s.sum(s));
            //將信息寫入文本文件中
            bw.write(sb.toString());
            //換行
            bw.newLine();
            //刷新流
            bw.flush();
        }
        //關閉流,釋放資源
        bw.close();
    }
}

控制臺截圖:
技術分享圖片
文本文件截圖:
技術分享圖片

2、已知s.txt文件中有這樣的一個字符串:“hcexfgijkamdnoqrzstuvwybpl”
請編寫程序讀取數據內容,把數據排序後寫入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集合/數組排序知識與IO流結合實例