【java】使用文本文档IO流代替数据库实现增删改查

实现效果是这样的
可以看到包含了增删改查和写入全部功能
【java】使用文本文档IO流代替数据库实现增删改查_第1张图片
在这里插入图片描述

直接上源码

复制粘贴然后在源码的根目录上创建一个Teacher.txt就可以直接用啦

主程序

import com.sun.source.tree.IfTree;

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

public class TManageSystem
{
    public String ManagerID = "admin";
    public String ManagerPassword = "123456";
    public static TeacherTable table = new TeacherTable(10);

    public static void main(String[] args)
    {
        TManageSystem TM = new TManageSystem();
        TM.readDBTable();
        TM.head();
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入账号");
        String loginID = scanner.nextLine();
        //System.out.println(loginID);

        System.out.println("请输入密码");
        String loginPassword = scanner.nextLine();
        while (!loginID.equals("admin") && !loginPassword.equals("123456"))
        {
            System.out.println("登录失败,账号或密码错误");
            System.out.println("请重新输入账号");
            loginID = scanner.nextLine();
            System.out.println("请重新输入密码");
            loginPassword = scanner.nextLine();
        }
        System.out.println("登录成功");
        table.showTable();
        System.out.println("功能介绍:\n" +
                "增 insert into 教师姓名 教师年龄 工号 职位 系别 主要科目 电话 地址(中间用空格隔开);\n"
                + "删 delect where 工号;\n" + "改 update 教师姓名 教师年龄 工号 职位 系别 主要科目 电话 地址 where 工号(中间用空格隔开);\n"
                + "查 select where 工号(工号不写就是全部);\n" +
                "写入文件 writer;\n" +
                "查看全部 selectAll;\n"
                + "退出系统 exit;");
        String listen = "";
        // table.addRow(new String[]{"教师姓名"," 教师年龄"," 工号"," 职位 ","系别"," 主要科目 ","电话 ","地址"});
        while (!listen.equals("exit"))
        {
            listen = scanner.nextLine();
            if (listen.isEmpty())
            {
                System.out.println("请输入命令");
            }
            else if (!listen.substring(listen.length() - 1, listen.length()).equals(";"))
            {
                System.out.println("指令最后要以 ; 结尾");

            }
            else
            {
                TM.Lexer(listen);
            }

            //System.out.println(listen.substring(listen.length()-1,listen.length()));

        }

    }
public void Writer(){
        table.delectEmptyLine();
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("Teacher.txt"));
        String temp = "";
        for (int i = 1; i < table.table.length; i++)
        {
            if (table.table[i][0].equals("0"))
            {
                break;
            }
            for (int j = 0; j < table.table[0].length; j++)
            {
                if (!table.table[i][j].equals("0"))
                {
                   temp+= table.table[i][j];
                }
            }
            temp+=".\n";


        }
        out.write(temp);
        out.close();
        System.out.println("写入成功!");
        table.index = 1;
        readDBTable();
    } catch (IOException e) {
        System.out.println(e);
    }
}
    public void head()
    {//填充表

        table.head(new String[]{"姓名", "年龄", "ID", "职称", "系别", "课程", "电话", "地址"});
        //table.addRow(new String[]{"老王", "18", "ax1122", "班主任", "计算机", "c语言设计", "15905967002", "北京海定"});
    }

    public void Lexer(String syntax)
    {//语法分析器
        String temp = "";
        String[] elements = getElemetStr(syntax);
        String[] temp2 = new String[8];
        int index = 0;
        for (int i = 0; i < elements.length; i++)
        {
            switch (elements[i])
            {
                case "writer ":
                    Writer();
                    return;
                case "insert ":
                    for (int j = 2; j <= 9; j++)
                    {
                       // System.out.println(index + "" + j);
                        temp2[index++] = elements[j];
                    }
                    //System.out.println(index);

                    table.addRow(temp2);
                    System.out.println("添加了一行");

                    return;
                case "delect ":
                    table.delete(elements[2]);
                    return;
                case "update ":
                    for (int j = 1; j <= 8; j++)
                    {
                        temp2[index++] = elements[j];
                    }
                    index = 0;
                    table.update(elements[10], temp2);
                    return;
                case "select ":
                    table.select(elements[2]);
                    return;
                case "selectAll ":
                    table.showTable();
                    return;
            }
        }
        System.out.println("找不到命令,请重新输入");
    }

    public void readDBTable()
    {
        try (FileReader fw = new FileReader("Teacher.txt"))
        {
            BufferedReader bufReader = new BufferedReader(fw);
            String line;
            String[] Row = new String[8];
            String temp = "";
            String bolen = "";
            int i = 0;
            int j = 0;
            while (bufReader.ready())
            {
                //System.out.println("已读");
                line = bufReader.readLine();
                bolen = line.substring(i, i + 1);
                while (!bolen.equals("."))
                {//若当前读取的字符不为‘.’则输出一行
                    //System.out.println(bolen);
                    if (!bolen.equals(" "))
                    {
                        bolen = line.substring(i, i + 1);
                        temp += line.substring(i, ++i);
                    }
                    else
                    {
                        bolen = line.substring(i, i + 1);
                        // System.out.println(temp);
                        Row[j++] = temp;
                        temp = "";
                    }
                }
                i = 0;
                j = 0;
                table.addRow(Row);
            }
        }
        catch (IOException e)
        {
            //throw new RuntimeException(e);
        }

    }

    public String[] getElemetStr(String line)
    {
        int i = 0;//当前字符的角标
        int j = 0;//当前元素的角标
        String[] tempArr = new String[20];
        for (int o = 0; o < tempArr.length; o++)
        {
            tempArr[o] = "";
        }
        String bolen = "";
        String temp = "";
        while (!bolen.equals(";"))
        {
            if (!bolen.equals(" "))
            {
                bolen = line.substring(i, i + 1);
                temp += line.substring(i, ++i);
            }
            else
            {
                bolen = line.substring(i, i + 1);
                tempArr[j++] = temp;
                temp = "";//初始化

            }

        }
        if (j == 0)
        {
            tempArr[j++] = temp;
        }
        if (bolen.equals(";"))
        {
            tempArr[j++] = temp.substring(0, temp.length() - 1) + " ";
            temp = "";//初始化

            i++;
        }
        String[] temp2 = new String[j];
        for (int k = 0; k < tempArr.length; k++)
        {
            if (!tempArr[k].equals(""))
            {
                temp2[k] = tempArr[k];
            }
        }
        for (int l = 0; l < temp2.length; l++)
        {
            //System.out.print(temp2[l]);
        }
        System.out.println();

        return temp2;
    }
}

表的实现

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TeacherTable
{
    public static String[][] table;
    public static int index = 1;

    //initial able
    public TeacherTable(int n)
    {

        table = new String[n][8];
        for (int i = 0; i < table.length; i++)
        {
            for (int j = 0; j < table[0].length; j++)
            {
                table[i][j] = "0";
            }
        }

    }

    //初始化表头
    public void head(String[] head)
    {
        for (int i = 0; i < head.length; i++)
        {
            table[0][i] = head[i];
        }
        index++;
    }

    //show table
    public void showTable()
    {
        delectEmptyLine();
        System.out.println(table[1][2]);
        for (int i = 0; i <= table.length - 1; i++)
        {
            for (int j = 0; j <= table[0].length - 1; j++)
            {
                //System.out.print(table[i][j] + "");

                if (!table[i][j].equals("0"))
                {
                    System.out.print(table[i][j] + "|");
               }else return;
            }
            System.out.println();
        }

    }

    public int foundRowInID(String ID)
    {
        for (int i = 1; i <= table[0].length; i++)
        {
            if (table[i][2].equals(ID))
            {
                //System.out.println(i);
                return i;
            }
        }
        return -1;

    }

    private int getEmptyLine()
    {
        for (int i = 0; i < table.length; i++)
        {
            //System.out.println(table[i][0]);
            if (table[i][0].equals("0"))
            {
                return i;
            }
        }
        return -1;
    }

    public void addRow(String[] values)
    {
        if (values.length > table[0].length)
        {
            System.out.println("添加的列超过索引");
            return;
        }
        String temp = "";
        for (int i = 0; i < values.length; i++)
        {
            table[index][i] = values[i];

            temp += values[i];
        }
        //System.out.println("表格" + index);
        index++;


    }

    public void update(String ID, String[] values)
    {//通过工号修改
        if (foundRowInID(ID) == -1)
        {
            System.out.println("找不到工号,重新输入");
            return;
        }
        for (int i = 0; i < table.length; i++)
        {//找到工号所在行
            if (!table[i][2].equals(""))
            {
                if (table[i][2].equals(ID))
                {
                    for (int j = 0; j < table[0].length; j++)
                    {//修改列
                        table[i][j] = values[j];

                    }
                    return;
                }
            }
            else
            {
                return;
            }
        }
    }

    public void delete(String ID)
    {
        int bolen = foundRowInID(ID);
        if (bolen == -1)
        {
            System.out.println("找不到工号,请重新输入");
            return;
        }
        for (int i = 0; i < table[0].length; i++)
        {
            table[bolen][i] = "0";
        }
        System.out.println("删除成功,改变一行");
        delectEmptyLine();
        index--;
    }

    public void select(String ID)
    {
        System.out.println(foundRowInID(ID));
        if (foundRowInID(ID) != -1)
        {
            for (int i = 0; i < table[0].length; i++)
            {

                System.out.print(table[foundRowInID(ID)][i] + "|");
            }
            System.out.println();
        }
        else
        {
            System.out.println("找不到工号,请重新输入");
        }
    }

    public void delectEmptyLine()
    {
        int bolen = getEmptyLine();
        if (bolen == -1)
        {
            return;
        }
        String[] temp = new String[table[0].length];

        //System.out.println("列宽"+table[0].length);
        // System.out.println("行高"+table.length);

        for (int i = bolen; i < table.length - 1; i++)
        {
            for (int j = 0; j < table[0].length; j++)
            {
                temp[j] = table[i + 1][j];
                table[i][j] = temp[j];
            }
        }
    }

}

你可能感兴趣的:(java,开发语言,数据库)