1. 程式人生 > >IO流讀寫檔案並排序

IO流讀寫檔案並排序

package com.drzhang.dao;

import java.util.Objects;

public class User implements Comparable {
    private String username;
    private Integer age;



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return Objects.equals(getUsername(), user.getUsername()) &&
                Objects.equals(getAge(), user.getAge());
    }

    @Override
    public int hashCode() {

        return Objects.hash(getUsername(), getAge());
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        User user = (User) o;
        if (this.age > user.age){
            return 1;
        }else if (this.age < user.age){
            return -1;
        }else {
            return this.username.compareTo(user.username);
        }
    }
}

package com.drzhang.dao;

import java.io.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class FileTest {
    public static void main(String[] args) throws IOException {
        User user;

        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\wwx155675\\Desktop\\usermanagercore\\a.txt"), "GBK");
        InputStreamReader inputStreamReader1 = new InputStreamReader(new FileInputStream("C:\\Users\\wwx155675\\Desktop\\usermanagercore\\b.txt"), "UTF-8");
        BufferedReader isr1 = new BufferedReader(inputStreamReader);
        BufferedReader isr2 = new BufferedReader(inputStreamReader1);


        Set<User> set = new TreeSet<>();

        int i = 0;
        String line = "";
        while ((line = isr1.readLine()) != null){
            i++;
            if (i < 3){
                continue;
            }
            user = new User();
            user.setUsername(line.split(" ")[0]);
            user.setAge(Integer.parseInt(line.split(" ")[1]));
            set.add(user);
        }
        i=0;
        isr1.close();


        while ((line = isr2.readLine()) != null){
            i++;
            if (i < 3){
                continue;
            }
            user = new User();
            user.setUsername(line.split(" ")[0]);
            user.setAge(Integer.parseInt(line.split(" ")[1]));
            set.add(user);
        }
        i=0;
        isr2.close();


      File writeName = new File("C:\\Users\\wwx155675\\Desktop\\usermanagercore\\c.txt");
      BufferedWriter bw = new BufferedWriter(new FileWriter(writeName));
      Iterator it = set.iterator();
      bw.write("學生測試\r\n" +
              "學生 成績");
      while (it.hasNext()){
          bw.newLine();
          User user1 = (User)it.next();
          bw.write(user1.getUsername()+" " + user1.getAge());
      }

      bw.flush(); // 把快取區內容壓入檔案
        bw.close();

    }
}