Java 开发篇+一个简单的数据库管理系统ZDB

说明:本文供数据库爱好者和初级开发人员学习使用
标签:数据库管理系统、RDBMS、Java小程序、Java、Java程序
系统:Windows 11 x86
CPU :Intel
IDE :IntelliJ IDEA Community Edition 2024
语言:Java语言
标准:JDK 17
提示:如果您发现本文哪里写的有问题或者有更好的写法请留言或私信我进行修改优化


★ 程序界面

★ 相关文章

✔ C语言 开发篇+一个简单的数据库管理系统ZDB
✔ C++ 开发篇+一个简单的数据库管理系统ZDB
 

★ 功能简介

目前该Java小程序实现了RDBMS上表的几个基础功能,如:
✔ DDL:create、drop、alter、truncate、flashback、rename、purge
✔ DML:insert、delete、update、select

★ 程序特点

✔ 程序会在首次运行时在同级创建一个数据文件和一个字典文件
✔ 程序运行期间表数据存储在内存中,类似于Redis
✔ 程序运行结束时会将表数据采用序列化和反序列化方式存取
✔ 程序采用List集合实现表中行和列的动态调整
✔ 程序采用Set 集合实现表集合(表空间)大小的动态调整
✔ 程序采用Map 集合实现数据库参数集合的动态调整
✔ 表在drop后purge前支持闪回,purge后相关空间会被清理
✔ 表记录在删除后只是标记为删除,方便后期复用并减少数据shrink的性能消化

★ 程序试用

https://download.csdn.net/download/zzt_2009/89102377

★ 使用案例

############【欢迎使用 ZDB】############
# 语言:Java 17          IDE:IntelliJ #
# 作者:zzt_2009         版本:V 7.0.0 #
# 主页:https://blog.csdn.net/zzt_2009 #
# [H/h]查看帮助          [E/e]退出程序 #
字典文件不存在,初始化中……
字典落盘成功
数据文件不存在,初始化中……
数据落盘成功
首次使用,将为您展示帮助信息:
# L  :列出所有表的信息     > 案例:L T0                #
# C  :创建表并指定列名     > 案例:C T0 ID NAME        #
# D  :删除表(支持闪回) 	> 案例:D T0                #
# A  :修改表的所有列名     > 案例:A T0 IDS NAMES      #
# R  :重命名表的名称       > 案例:R T0 T00            #
# T  :清空表中的数据       > 案例:T T0                #
# F  :闪回被删除的表       > 案例:F T0_$0             #
# P  :清空被删除的表       > 案例:P                   #
# i  :往表中插入1行数据    > 案例:i T0 122 Traffic    #
# d  :从表中删除1行数据    > 案例:d T0 3              #
# u  :更新表中的1行数据    > 案例:u T0 3 122 Traffic  #
# s  :查看表中的有效数据   > 案例:s T0                #
# S  :查看表中的所有数据   > 案例:S T0                #
# c  :清理屏幕             > 案例:c                   #
# H/h:查看帮助信息         > 案例:h                   #
# E/e:退出程序             > 案例:e                   #
SQL > L
表名: T0	编号: 0	状态: 2	Rows: 3	Cols: 2 [ TEL/NAME/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:2
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > C T2 ID NAME
表 T2 创建成功。
字典落盘成功
数据落盘成功
表名: T2	编号: 2	状态: 1	Rows: 0	Cols: 2 [ ID/NAME/ ]
表名: T0	编号: 0	状态: 2	Rows: 3	Cols: 2 [ TEL/NAME/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:3
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > C T3 ID NAME COMMENT
表 T3 创建成功。
字典落盘成功
数据落盘成功
表名: T2	编号: 2	状态: 1	Rows: 0	Cols: 2 [ ID/NAME/ ]
表名: T0	编号: 0	状态: 2	Rows: 3	Cols: 2 [ TEL/NAME/ ]
表名: T3	编号: 3	状态: 1	Rows: 0	Cols: 3 [ ID/NAME/COMMENT/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:4
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > D T2
表 T2 删除成功。
字典落盘成功
数据落盘成功
表名: T2_$2	编号: 2	状态: 3	Rows: 0	Cols: 2 [ ID/NAME/ ]
表名: T0	编号: 0	状态: 2	Rows: 3	Cols: 2 [ TEL/NAME/ ]
表名: T3	编号: 3	状态: 1	Rows: 0	Cols: 3 [ ID/NAME/COMMENT/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:4
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > F T2_$2
表 T2_$2 已被恢复。
字典落盘成功
数据落盘成功
表名: T2_$2	编号: 2	状态: 1	Rows: 0	Cols: 2 [ ID/NAME/ ]
表名: T0	编号: 0	状态: 2	Rows: 3	Cols: 2 [ TEL/NAME/ ]
表名: T3	编号: 3	状态: 1	Rows: 0	Cols: 3 [ ID/NAME/COMMENT/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:4
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > R T2_$2 T2
表 T2_$2 更名为 T2 成功。
字典落盘成功
数据落盘成功
表名: T2	编号: 2	状态: 1	Rows: 0	Cols: 2 [ ID/NAME/ ]
表名: T0	编号: 0	状态: 2	Rows: 3	Cols: 2 [ TEL/NAME/ ]
表名: T3	编号: 3	状态: 1	Rows: 0	Cols: 3 [ ID/NAME/COMMENT/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:4
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > s T0
行号:0	数据:[H, TEL, NAME]
行号:1	数据:[A, 110, police]
行号:2	数据:[A, 120, ambulance]
行号:3	数据:[A, 119, fire]
SQL > d T0 2
相关行数据已删除。
字典落盘成功
数据落盘成功
行号:0	数据:[H, TEL, NAME]
行号:1	数据:[A, 110, police]
行号:2	数据:[A, 119, fire]
SQL > S T0
行号:0	数据:[H, TEL, NAME]
行号:1	数据:[A, 110, police]
行号:2	数据:[D, 120, ambulance]
行号:3	数据:[A, 119, fire]
SQL > i T0 122 car
0 / H
1 / A
2 / D
数据插入表 T0 成功。
字典落盘成功
数据落盘成功
行号:0	数据:[H, TEL, NAME]
行号:1	数据:[A, 110, police]
行号:2	数据:[A, 122, car]
行号:3	数据:[A, 119, fire]
SQL > u T0 2 120 doctor
数据更新成功。
字典落盘成功
数据落盘成功
行号:0	数据:[H, TEL, NAME]
行号:1	数据:[A, 110, police]
行号:2	数据:[A, 120, doctor]
行号:3	数据:[A, 119, fire]
SQL > T T0
表 T0 已被截断。
字典落盘成功
数据落盘成功
表名: T2	编号: 2	状态: 1	Rows: 0	Cols: 2 [ ID/NAME/ ]
表名: T0	编号: 0	状态: 2	Rows: 0	Cols: 2 [ TEL/NAME/ ]
表名: T3	编号: 3	状态: 1	Rows: 0	Cols: 3 [ ID/NAME/COMMENT/ ]
表名: T1	编号: 1	状态: 2	Rows: 3	Cols: 2 [ country/city/ ]
对象的nextvalue:4
状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
SQL > S T0
行号:0	数据:[H, TEL, NAME]
SQL > e
字典落盘成功
数据落盘成功
#########【感谢使用 ZDB 再见!】#########

★ 程序源码

package zdb_default;

import java.io.*;
import java.util.*;

// 字典类(程序较为简单为了减少文件切换使用内部类方式)
class Class_Dict implements Serializable { // 实现 Serializable 接口,这是进行对象序列化和反序列化所必需的。
    private static final long serialVersionUID = 1L; // 为了在版本升级时保持版本的兼容性,在序列化类中添加 serialVersionUID
    public int value;       //值
    public String comment;  //描述

    // 添加构造函数和getter/setter方法(如果需要)
    public Class_Dict(int value, String comment) {
        this.value = value;
        this.comment = comment;
    }

    // 如果需要,还可以添加toString方法
    @Override
    public String toString() {
        return "Class_Dict{" +
                "value=" + value +
                ", comment='" + comment + '\'' +
                '}';
    }
}

// 表类(程序较为简单为了减少文件切换使用内部类方式)
class Class_Table implements Serializable { // 实现 Serializable 接口,这是进行对象序列化和反序列化所必需的。
    private static final long serialVersionUID = 1L; // 为了在版本升级时保持版本的兼容性,在序列化类中添加 serialVersionUID
    public int id;            // 序号
    public int state;         // 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
    public String name;       // 名称
    public int oid;           // 对象编号
    public int rows;          // 行数
    public int cols;          // 列数
    public List<List<String>> record = new ArrayList<>(); // 数据&状态:"">未分配、H>字段名、A>可用、D>删除
}

public class Class_Main {
    // 定义常量
    private static final String DICT_FILE = "ZDB.dic";
    private static final String DATA_FILE = "ZDB.dat";
    // 创建一个Set集合来存储Class_Dict对象(参数)
    private static Map<String, Class_Dict> dicts = new HashMap<>();
    // 创建一个Set集合来存储Class_Table对象(表)
    private static Set<Class_Table> tables = new HashSet<>();

    public static void main(String[] args) {
        // 显示头信息
        printHead();
        // 从文件中读取字典信息到集合中
        readDicts();
        // 从文件中读取表信息到集合中
        tables = readTables();
        // 读取输入
        while (true) {
            String table_name;
            System.out.print("SQL > ");
            Scanner scanner = new Scanner(System.in);
            String command = scanner.nextLine();
            String[] tokens = command.split(" ");
            String operation = tokens[0];
            switch (operation) {
                case "C": // 创建新表,用法:C T0 id name
                    createTable(tokens);
                    break;
                case "D": // 删除表,用法:D T0
                    dropTable(tokens);
                    break;
                case "A": // 修改表列名,用法:A T0 new_id new_name
                    alterTable(tokens);
                    break;
                case "R": // 重命名表,用法:R T0 T00
                    renameTable(tokens);
                    break;
                case "T": // 清空表数据,用法:T T0
                    truncateTable(tokens);
                    break;
                case "F": // 闪回删除的表,用法:F T0
                    flashbackTable(tokens);
                    break;
                case "P": // 清空所有已删除的表,用法:P
                    purgeTables();
                    break;
                case "L": // 列出库中所有表名和其基础信息,用法:L
                    listTables();
                    break;
                case "i": // 往表插入数据,用法:i T0 1 a 2 b 3 c
                    insertTable(tokens);
                    break;
                case "d": // 删除表中第N行数据,用法:d T0 N
                    deleteTable(tokens);
                    break;
                case "u": // 更新表中第N行数据,用法:u T0 N 1 a
                    updateTable(tokens);
                    break;
                case "s": // 查询表中所有可见数据,用法:s T0
                    selectTable(tokens);
                    break;
                case "S": // 查询表中所有数据,用法:S T0
                    selectAllTable(tokens);
                    break;
                case "c": // 清理屏幕并重新显示printHead()
                    printHead();
                    break;
                case "h": //
                case "H": //
                    printHelp();
                    break;
                case "e": //
                case "E": //
                    writeDicts();
                    writeTables();
                    printEnd();
                    System.exit(0);
                default:
                    System.out.println("命令不正确请重新输入,或按[H/h]查看帮助");
                    break;
            }
        } //循环结束
    } //main_end


    public static void createTable(String[] tokens) {
        if (tokens.length < 3) {
            System.out.println("输入有误,案例:C T0 ID NAME");
            return;
        }
        String tableName = tokens[1];
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                System.out.println("表 " + tableName + " 已存在。");
                return;
            }
        }
        Class_Table newTable = new Class_Table();
        newTable.state = 1;
        newTable.name = tableName;
        newTable.oid = dicts.get("noid").value;
        newTable.rows = 0;
        newTable.cols = (tokens.length - 2);
        dicts.put("noid", new Class_Dict((dicts.get("noid").value + 1), "对象编号的nextvalue"));
        List<String> header = new ArrayList<>();
        header.add("H");
        header.addAll(Arrays.asList(tokens).subList(2, tokens.length));
        newTable.record.add(header);
        tables.add(newTable);
        System.out.println("表 " + tableName + " 创建成功。");
        writeDicts();
        writeTables();
        listTables();
    }

    public static void dropTable(String[] tokens) {
        if (tokens.length != 2) {
            System.out.println("输入有误,案例:D T0");
            return;
        }
        String tableName = tokens[1];
        boolean found = false;
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                table.state = 3;
                table.name = table.name + "_$" + table.oid;
                found = true;
                break;
            }
        }
        if (found) {
            System.out.println("表 " + tableName + " 删除成功。");
            writeDicts();
            writeTables();
            listTables();
        } else {
            System.out.println("表 " + tableName + " 不存在或已被删除。");
        }
    }

    public static void renameTable(String[] tokens) {
        if (tokens.length != 3) {
            System.out.println("命令错误,请检查参数数量。");
            return;
        }
        String oldName = tokens[1];
        String newName = tokens[2];
        for (Class_Table table : tables) {
            if ((table.name.equals(oldName)) && ((table.state == 1) || (table.state == 2))) {
                table.name = newName;
                System.out.println("表 " + oldName + " 更名为 " + newName + " 成功。");
                writeDicts();
                writeTables();
                listTables();
                return;
            }
        }
        System.out.println("表 " + oldName + " 不存在。");
    }

    public static void alterTable(String[] tokens) {
        if (tokens.length < 3) {
            System.out.println("输入有误,案例:A T0 new_col1 new_col2");
            return;
        }
        String tableName = tokens[1];
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                if ((tokens.length - 2) == table.cols) {
                    List<String> header = new ArrayList<>();
                    header.add("H");
                    header.addAll(Arrays.asList(tokens).subList(2, tokens.length));
                    table.record.set(0, header);
                    writeDicts();
                    writeTables();
                    listTables();
                } else {
                    System.out.println("新列数量须和旧列数量一致!");
                }
                return;
            }
        }
        System.out.println("表 " + tableName + " 不存在。");
    }


    public static void truncateTable(String[] tokens) {
        if (tokens.length != 2) {
            System.out.println("输入有误,案例:T T0");
            return;
        }
        String tableName = tokens[1];
        boolean tableFound = false;
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                tableFound = true;
                List<String> header = table.record.get(0);
                table.record.clear();
                table.rows = 0;
                table.record.add(header);
                System.out.println("表 " + tableName + " 已被截断。");
                writeDicts();
                writeTables();
                listTables();
                break;
            }
        }
        if (!tableFound) {
            System.out.println("表 " + tableName + " 不存在。");
        }
    }

    public static void flashbackTable(String[] tokens) {
        if (tokens.length != 2) {
            System.out.println("输入有误,案例:F T0");
            return;
        }
        String tableName = tokens[1];
        boolean tableFound = false;
        for (Class_Table table : tables) {
            if (table.name.equals(tableName) && table.state == 3) {
                tableFound = true;
                if (table.rows == 0) {
                    table.state = 1;
                } else {
                    table.state = 2;
                }
                System.out.println("表 " + tableName + " 已被恢复。");
                writeDicts();
                writeTables();
                listTables();
                break;
            }
        }
        if (!tableFound) {
            System.out.println("表 " + tableName + " 不存在或未被删除。");
        }
    }

    public static void purgeTables() {
        tables.removeIf(table -> table.state == 3);
        System.out.println("所有标记为删除的表已清除。");
    }

    public static void listTables() {
        tables.forEach(table -> {
            System.out.print("表名: " + table.name + "\t");
            System.out.print("编号: " + table.oid + "\t");
            System.out.print("状态: " + table.state + "\t");
            System.out.print("Rows: " + table.rows + "\t");
            System.out.print("Cols: " + table.cols + " ");
            System.out.print("[ ");
            for (int i = 1; i < table.record.get(0).size(); i++) {
                System.out.print(table.record.get(0).get(i) + "/");
            }
            System.out.println(" ]");
        });
        System.out.println("对象的nextvalue:" + dicts.get("noid").value);
        System.out.println("状态:0>未分配、1>新表、2>有数、3>删除、4>可复用");
    }

    public static void insertTable(String[] tokens) {
        if (tokens.length < 3) {
            System.out.println("输入有误,案例:i T0 911 fire");
            return;
        }
        String tableName = tokens[1];
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                if ((tokens.length - 2) == table.cols) {
                    for (int i = 0; i < table.record.size(); i++) {
                        System.out.println(i + " / " + table.record.get(i).get(0));
                        if (Objects.equals(table.record.get(i).get(0), "D")) {
                            List<String> rowData = new ArrayList<>();
                            rowData.add(0, "A");
                            rowData.addAll(Arrays.asList(tokens).subList(2, tokens.length));
                            table.record.set(i, rowData);
                            table.rows++;
                            System.out.println("数据插入表 " + tableName + " 成功。");
                            writeDicts();
                            writeTables();
                            selectTable(("X " + tableName).split(" "));
                            return;
                        }
                    }
                    List<String> rowData = new ArrayList<>();
                    rowData.add(0, "A");
                    rowData.addAll(Arrays.asList(tokens).subList(2, tokens.length));
                    table.record.add(rowData);
                    table.rows++;
                    System.out.println("数据插入表 " + tableName + " 成功。");
                    writeDicts();
                    writeTables();
                    selectTable(("X " + tableName).split(" "));
                } else {
                    System.out.println("列数量不一致!");
                }
                return;
            }
        }
        System.out.println("表 " + tableName + " 不存在。");
    }

    public static void deleteTable(String[] tokens) {
        if (tokens.length != 3) {
            System.out.println("输入有误,案例:d T0 1");
            return;
        }
        String tableName = tokens[1];
        String rowNumber = tokens[2];
        int irowNumber = Integer.parseInt(rowNumber);
        boolean tableFound = false;
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                if ((irowNumber != 0) && (irowNumber <= table.rows)) {
                    tableFound = true;
                    table.record.get(irowNumber).set(0, "D");
                    table.rows--;
                    System.out.println("相关行数据已删除。");
                    writeDicts();
                    writeTables();
                    selectTable(("X " + tableName).split(" "));
                    return;
                } else {
                    System.out.println("不可操作的行号。");
                    return;
                }
            }
        }
        if (!tableFound) {
            System.out.println("表 " + tableName + " 不存在。");
        }
    }

    public static void updateTable(String[] tokens) {

        if (tokens.length < 4) {
            System.out.println("输入有误,案例:u T0 1 110 police");
            return;
        }
        String tableName = tokens[1];
        String rowNumber = tokens[2];
        int irowNumber = Integer.parseInt(rowNumber);
        boolean tableFound = false;
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                tableFound = true;
                if (table.record.get(irowNumber).get(0).equals("A")) {
                    if ((tokens.length - 3) == table.cols) {
                        List<String> rowData = new ArrayList<>();
                        rowData.add(0, "A");
                        rowData.addAll(Arrays.asList(tokens).subList(3, tokens.length));
                        table.record.set(irowNumber, rowData);
                        System.out.println("数据更新成功。");
                        writeDicts();
                        writeTables();
                        selectTable(("X " + tableName).split(" "));
                    } else {
                        System.out.println("列数量不一致!");
                    }
                    return;
                }
            }
        }
        if (!tableFound) {
            System.out.println("表 " + tableName + " 不存在。");
        }
    }

    public static void selectTable(String[] tokens) {
        if (tokens.length != 2) {
            System.out.println("输入有误,案例:s T0");
            return;
        }
        String tableName = tokens[1];
        boolean tableFound = false;
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                tableFound = true;
                int i = 0;
                for (List<String> row : table.record) {
                    if ((row.get(0).equals("H")) || (row.get(0).equals("A"))) {
                        System.out.println("行号:" + i + "\t数据:" + row);
                        i++;
                    }
                }
                break;
            }
        }
        if (!tableFound) {
            System.out.println("表 " + tableName + " 不存在。");
        }
    }


    public static void selectAllTable(String[] tokens) {
        if (tokens.length != 2) {
            System.out.println("输入有误,案例:s T0");
            return;
        }
        String tableName = tokens[1];
        boolean tableFound = false;
        for (Class_Table table : tables) {
            if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
                tableFound = true;
                int i = 0;
                for (List<String> row : table.record) {
                    System.out.println("行号:" + i + "\t数据:" + row);
                    i++;
                }
                break;
            }
        }
        if (!tableFound) {
            System.out.println("表 " + tableName + " 不存在。");
        }
    }

    public static void printHead() {
        try {
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("############【欢迎使用 ZDB】############");
        System.out.println("# 语言:Java 17          IDE:IntelliJ #");
        System.out.println("# 作者:zzt_2009         版本:V 7.0.0 #");
        System.out.println("# 主页:https://blog.csdn.net/zzt_2009 #");
        System.out.println("# [H/h]查看帮助          [E/e]退出程序 #");
    }

    public static void printEnd() {
        System.out.println("#########【感谢使用 ZDB 再见!】#########");
    }

    public static void printHelp() {
        System.out.println("# L  :列出所有表的信息     > 案例:L T0                #");
        System.out.println("# C  :创建表并指定列名     > 案例:C T0 ID NAME        #");
        System.out.println("# D  :删除表(支持闪回)   > 案例:D T0                #");
        System.out.println("# A  :修改表的所有列名     > 案例:A T0 IDS NAMES      #");
        System.out.println("# R  :重命名表的名称       > 案例:R T0 T00            #");
        System.out.println("# T  :清空表中的数据       > 案例:T T0                #");
        System.out.println("# F  :闪回被删除的表       > 案例:F T0_$0             #");
        System.out.println("# P  :清空被删除的表       > 案例:P                   #");
        System.out.println("# i  :往表中插入1行数据    > 案例:i T0 122 Traffic    #");
        System.out.println("# d  :从表中删除1行数据    > 案例:d T0 3              #");
        System.out.println("# u  :更新表中的1行数据    > 案例:u T0 3 122 Traffic  #");
        System.out.println("# s  :查看表中的有效数据   > 案例:s T0                #");
        System.out.println("# S  :查看表中的所有数据   > 案例:S T0                #");
        System.out.println("# c  :清理屏幕             > 案例:c                   #");
        System.out.println("# H/h:查看帮助信息         > 案例:h                   #");
        System.out.println("# E/e:退出程序             > 案例:e                   #");
    }


    public static void initDicts() {
        dicts.put("noid", new Class_Dict(2, "对象编号的nextvalue"));
    }


    public static void writeDicts() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DICT_FILE))) {
            oos.writeObject(dicts);
            System.out.println("字典落盘成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void readDicts() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DICT_FILE))) {
            dicts = (Map<String, Class_Dict>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("字典文件不存在,初始化中……");
            initDicts();
            writeDicts();
        }
    }


    public static void initTables() {
        Class_Table T0 = new Class_Table();
        T0.id = 0;
        T0.state = 2;
        T0.name = "T0";
        T0.oid = 0;
        T0.rows = 3;
        T0.cols = 2;
        T0.record.add(new ArrayList<>());
        T0.record.get(0).add("H");
        T0.record.get(0).add("TEL");
        T0.record.get(0).add("NAME");
        T0.record.add(new ArrayList<>());
        T0.record.get(1).add("A");
        T0.record.get(1).add("110");
        T0.record.get(1).add("police");
        T0.record.add(new ArrayList<>());
        T0.record.get(2).add("A");
        T0.record.get(2).add("120");
        T0.record.get(2).add("ambulance");
        T0.record.add(new ArrayList<>());
        T0.record.get(3).add("A");
        T0.record.get(3).add("119");
        T0.record.get(3).add("fire");
        tables.add(T0);

        Class_Table T1 = new Class_Table();
        T1.id = 1;
        T1.state = 2;
        T1.name = "T1";
        T1.oid = 1;
        T1.rows = 3;
        T1.cols = 2;
        T1.record.add(new ArrayList<>());
        T1.record.get(0).add("H");
        T1.record.get(0).add("country");
        T1.record.get(0).add("city");
        T1.record.add(new ArrayList<>());
        T1.record.get(1).add("A");
        T1.record.get(1).add("China");
        T1.record.get(1).add("Beijing");
        T1.record.add(new ArrayList<>());
        T1.record.get(2).add("A");
        T1.record.get(2).add("America");
        T1.record.get(2).add("Washington");
        T1.record.add(new ArrayList<>());
        T1.record.get(3).add("A");
        T1.record.get(3).add("Russia");
        T1.record.get(3).add("Moscow");
        tables.add(T1);
    }

    public static void writeTables() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
            oos.writeObject(tables);
            System.out.println("数据落盘成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public static HashSet<Class_Table> readTables() {
        Set<Class_Table> loadedTables = new HashSet<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
            loadedTables = (Set<Class_Table>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("数据文件不存在,初始化中……");
            initTables();
            writeTables();
            loadedTables = readTables();
            System.out.println("首次使用,将为您展示帮助信息:");
            printHelp();
        }
        return (HashSet<Class_Table>) loadedTables;
    }

} //Class_Main_end

//end

※ 如果您觉得文章写的还不错, 别忘了在文末给作者点个赞哦 ~

20200426194203245.gif

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值