天天看點

IO實戰篇:資料排序處理深入 | 帶你學《Java語言進階特性》之七十六

上一篇:IO實戰篇:資料排序處理 | 帶你學《Java語言進階特性》之七十五

在上一節中我們實作了簡單的輸入一些資料并完成資料排序處理的功能,本節将進一步開發,使其實作将資料儲存進檔案并進行顯示的功能。

【本節目标】

通過閱讀本節内容,你将繼續複習檔案IO的相關内容,結合其他相關知識,對資料進行嚴格的結構化限制輸入後實作資料的正确的處理,進而實作将資料儲存進檔案并能夠全部顯示的功能。

資料排序處理深入

将上一題的内容進行擴充,可以将全部輸入的資訊儲存在檔案中,還可以添加資訊,并可以顯示全部的資料。

如果此時要進行内容的儲存,那麼首先一定要确認好所有輸入資料的儲存位置,所有的資料之間如果要想沿用之前的設計的結構,則資料檔案裡面的儲存應該做到格式統一,即:“姓名:成績|”的形式進行存儲,而在進行資料添加的時候可以添加兩類資料:“單獨的内容”、“一組内容”。還有一個前提:暫時不去考慮數過大的問題。

1、 設定一個檔案的處理類,該類之中隻是提供有資料的增加以及讀取

public class FileUtil {
    public static String load(File file) {
        Scanner scan = null;
        try{
            scan new Scanner(file);   //進行檔案加載
            if(scan.hasNext()) {
                String str = scan.next();  //擷取資料
                return str;
            }
            return null;
        }catch(Exception e){
            return null;
        }finally{
            if(scan != null) {
                scan.close();
            }
        }
    }
    public static void boolean append(File file,String content) {
        PrintStream out = null ;
        try{
            out = new PrintStream(new FileOutputStream(file,true));
            out.print(content);  //内容追加
            return true;
        }catch (Exception e){
            return false;
        }finally{
             if(out != null) {
                  out.close();
             }
        }
    }
}           

2、 擴充IStudentService操作方法

public interface IStudentService {
    public void append(String str);  //追加資料并且儲存到檔案
    public Student[] getData();  //擷取排序資料
}           

3、 修改StudentServiceImpl中的功能

public class StudentServiceImpl implements IStudentService {
    private static final File SAVE_FILE = new File("D:" + File.separator + "student.txt");
    private String content ;
    private Student [] students;
    public StudentServiceImpl() {
        this.content = FileUtil.load(SAVE_FILE);   //讀取已有的資料檔案
        this.handle();   //進行資料處理
    }
    @Override
    public void append(String str){
        if(str.startwith("|")) {    //最前面有|
            str = str.substring(1);    //截取後面的部分
        }
        if(!str.endwith("|")) {    //資料合理,可以直接追加
            str = str + "|";    //與後面的資料進行分割
        }
        FileUtil.append(SAVE_FILE,str);     //資料的追加處理
    }  
    private void handle(){   //進行字元串資料的處理操作
        if(this.content == null || "".equals(this.content)) {
            return ;  //沒有資料可以處理
        }
        String result[] = this.content.split("\\|") ;  //拆分資料
        this.student = new Student[result.length];
        for (int x = 0 ;x < result.length ; x ++) {
            String temp [] = result[x].split(":");
            this.students[x] = new Student(temp[0],Double.parseDouble(temp[1])) ;
        }
    }
    @Override
    public Student[] getData() {
        Arrays.sort(this.students);
        return this.students;
    }
}           

4、 此時工廠類不再需要輸入資料:

public class Factory{
    private Factory(){}
    public static IStudentService getInstance(){
        return new StudentServiceImpl());
    }
}           

5、 定義一個菜單處理

public class Menu {
    public Menu() {
        this.choose();
    }
    public void choose() {
        this.show();
        String choose = InputUtil.getStr("請進行選擇");
        switch (choose) {
            case "1":{  //接收輸入資料
                String str = InputUtil.getStr("請輸入要追加的資料:");
                IStudentService studentService = Factory.getInstance();
                studentService.append(str);  //追加資料
                choose();//重複出現
            }
            case "2":{  //顯示資料
                IStudentService studentService = Factory.getInstance();
                System.out.println(Array.toString(studentService.getData()));
                choose();//重複出現
            }
            case "0":{
                System.out.println("下次再見,拜拜!");
                System.exit(1);
            }
            default:{
                System.out.println("您輸入了非法的選項,無法進行處理,歉意确認後再次執行程式!");
                choose();
            }
        }
    }
    public void show() {
        System.out.println("【1】追加字元串資料\n");
        System.out.println("【2】顯示所有的學生資料\n");
        System.out.println("【0】結束程式執行。");
        System.out.println("\n\n\n");
    }
}           

6、 編寫測試類

public class IOCaseDemo {
    public static void main(String[] args) {
        new Menu();
    }
}           

執行結果:

【1】追加字元串資料

【2】逆序顯示所有的字元串資料

【0】結束程式執行。

請進行選擇:1

請輸入要追加的資料:Tom:78.1

請進行選擇1

請輸入要追加的資料:Jerry:98.1|tony:99.2

請進行選擇:2

[姓名:tony、成績:99.2,姓名:Jerry、成績:98.1,姓名:Tom、成績:78.1]

想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。

本内容視訊來源于

阿裡雲大學 下一篇:IO實戰篇:奇偶數統計 | 帶你學《Java語言進階特性》之七十七 更多Java面向對象程式設計文章檢視此處