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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/526275.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

“AI+信创”两翼齐飞,实在智能全面加速自主可控实在智能RPA

近日&#xff0c;实在智能牵手华为昇腾、摩尔线程在信创领域展开紧密合作&#xff0c;共同加速推进AI和信创产业创新发展。 华为昇腾与实在智能达成昇腾原生大模型联合创新合作&#xff0c;基于华为昇腾AI自主创新软硬件平台全栈技术、实在智能自研RPA基础大模型解决方案能力&a…

简单好用高效的视频补帧软件:Squirrel-RIFE

Squirrel-RIFE&#xff0c;轻松实现高效补帧&#xff0c;让您的视频画面瞬间流畅升级&#xff01;- 精选真开源&#xff0c;释放新价值。 概览 在观看视频内容的过程中&#xff0c;尤其是面对复杂多变的动画场景或高速运动镜头时&#xff0c;观众时常会遭遇视频帧率不足所引发…

算法中的二阶差分

众所周知&#xff0c;在往区间的每一个数都加上一个相同的数k&#xff0c;进行n次后会得到一个新的数列&#xff0c;如果每次加都循环区间挨个数加上k&#xff0c;这样时间复杂度无疑是O(n^2)&#xff0c;很高。这时可以采用一阶差分就可解决&#xff0c;这里默认会一阶差分&am…

物联网行业趋势——青创智通

工业物联网解决方案-工业IOT-青创智通 随着科技的不断进步和应用场景的日益扩大&#xff0c;物联网行业呈现出迅猛发展的势头。作为当今世界最具前瞻性和战略意义的领域之一&#xff0c;物联网行业的趋势和未来发展值得深入探讨。 ​一、物联网行业正逐渐实现全面普及。随着物…

鸿蒙ArkUI开发实战:制作一个【简单计数器】

构建第一个页面 使用文本组件 工程同步完成后&#xff0c;在 Project 窗口&#xff0c;点击 entry > src > main > ets > pages &#xff0c;打开 Index.ets 文件&#xff0c;可以看到页面由 Row 、 Column 、 Text 组件组成。 index.ets 文件的示例如下&#xff1…

飞机降落(区间问题)

思路&#xff1a; 受P1803 凌乱的yyy / 线段覆盖的启发。 对于这道题&#xff0c;我的第一想法不是dfs&#xff0c;而是把它看作区间来看&#xff0c;分别就是【t&#xff0c;tl】和【td&#xff0c;tdl】。先按照结束时间排序&#xff0c;先用第一个飞机不延迟降落的时间a[0…

制造业智能化一体式I/O模块的集成与应用案例分享

在现代制造业中&#xff0c;智能化一体式I/O模块的应用已经成为提升生产效率、优化工艺流程的关键技术之一。这种一体化I/O模块的主要功能在于作为PLC&#xff08;可编程逻辑控制器&#xff09;系统的扩展接口&#xff0c;以满足多样化的输入输出需求。本文将通过一个实际案例&…

DFS-0与异或问题,有奖问答,飞机降落

代码和解析 #include<bits/stdc.h> using namespace std; int a[5][5]{{1,0,1,0,1}}; //记录图中圆圈内的值&#xff0c;并初始化第1行 int gate[11]; //记录10个逻辑门的一种排列 int ans; //答案 int logic(int x, int y, int op){…

麒麟系统下安装qt5.9.1后不能输入中文

引言 在虚拟机上安装麒麟系统后,安装了qt5.9.1,只能输入英文和数字不能输入中文注释,编译的程序也不能输入中文。 原因 安装后的麒麟系统自带搜狗输入法,原本可以输入中文,但是qt5.9.1缺少支持搜狗输入法的fcitx插件。所以qt5.9.1中不能输入中文。 解决方法 安装fcit…

逆向入门:为CTF国赛而战day03

今天来做几道题目。 环境准备&#xff1a;ida ,Exeinfo,万能脱壳器&#xff08;后面有写资源&#xff09; 强推&#xff0c;亲测有效CTF小工具下载整理_ctf工具御剑下载-CSDN博客 [网站BUUCTF] 目录 题目一 题目二三 题目4&#xff1a;新年快乐 题目一 easyre题解_easyr…

在自定义数据集上微调 YOLOv9 模型

在自定义数据集上微调 YOLOv9模型可以显着提高目标检测性能,但这种改进有多显着呢?在这次全面的探索中,YOLOv9在SkyFusion数据集上进行了微调,分为三个不同的类别:飞机、船舶和车辆。通过一系列广泛的实验,包括修改学习率、图像大小和战略性冻结主干网,已经实现了令人印…

目标检测——RCNN系列学习(一)

前置知识 包括&#xff1a;非极大值抑制&#xff08;NMS&#xff09;、selective search等 RCNN [1311.2524] Rich feature hierarchies for accurate object detection and semantic segmentation (arxiv.org)https://arxiv.org/abs/1311.2524 1.网络训练 2.推理流程 3.总…

【数据库事务并发问题】脏读、丢失修改、不可重复读、幻读

文章目录 一、脏读二、丢失修改三、不可重复读四、幻读 一、脏读 第二个事务读了①修改的数据后&#xff0c;前一个事务回滚了 一个事务读取数据并且对数据进行了修改&#xff0c;这个修改对其他事务来说是可见的&#xff0c;即使当前事务没有提交。这时另外一个事务读取了这个…

幸运数(蓝桥杯)

该 import java.util.*; public class Main {public static void main(String[] args) {Scanner scannew Scanner(System.in);int cnt0;for(int i1;i<100000000;i) {String si"";int lens.length();if(len%2!0) continue;int sum10; //左边int sum20; //右边fo…

【Mybatis】Mybatis 二级缓存全详解教程

【Mybatis-Plus】Mybatis-Plus 二级缓存全详解 一&#xff0c;Mybatis-Plus介绍 MyBatis-Plus&#xff08;简称MP&#xff09;是一个基于 MyBatis 的增强工具&#xff0c;它简化了 MyBatis 的开发&#xff0c;并且提供了许多便利的功能&#xff0c;帮助开发者更高效地进行持久…

申请专利有用吗 好处

申请专利&#xff1a;一项值得考虑的策略 随着科技的快速发展和市场竞争的日益激烈&#xff0c;创新成为了企业或个人取得竞争优势的关键。在这样的背景下&#xff0c;申请专利成为了许多创新者保护自己创意和技术的重要手段。 申请专利真的有用吗&#xff1f; 申请专利可以…

CS162 Operating System笔记

What is an Operating System? it’s typically a special layer of software that provides the application access to hardware resources.So.it’s convenient abs fractions of complex hardware devices.

数位递增数-第12届蓝桥杯选拔赛Python真题精选

[导读]&#xff1a;超平老师的Scratch蓝桥杯真题解读系列在推出之后&#xff0c;受到了广大老师和家长的好评&#xff0c;非常感谢各位的认可和厚爱。作为回馈&#xff0c;超平老师计划推出《Python蓝桥杯真题解析100讲》&#xff0c;这是解读系列的第46讲。 数位递增数&#…

【软考】23种设计模式详解,记忆方式,并举例说明

23种设计模式详解&#xff0c;举例说明 一、创建型模式1.1、抽象工厂模式&#xff08;Abstract Factory&#xff09;1.1.1、简介1.1.2、意图与应用场景1.1.3、结构1.1.4、优缺点1.1.4、示例代码&#xff08;简化版&#xff09; 1.2、建造者模式&#xff08;Builder&#xff09;…

微信小程序-接入sse数据流并实现打字机效果( ChatGPT )

从流中获取的数据格式如下 小程序调用SSE接口 const requestTask wx.request({url: xxx, // 需要请求的接口地址enableChunked: true, // enableChunked必须为truemethod: "GET",timeout: 120000,success(res) {console.log(res.data)},fail: function (error) {//…
最新文章