OperationTextTest

我们以前都是在数据库中实现增删查改等功能,现在的数据库做的很好了,增删查改功能用一些别人已经封装好的方法就可以实现,这里就不说数据库实现增删查改了,因为它so easy!。我们来在文本文件上进行增删查改功能,支持实时刷新。听到这是不是有想去实现的冲动,因为文本文件是静态的,怎样去刷新?我先给出下面所写出来的一种方法吧。还可以先把所有操作后的数据放到HashMap中再将HashMap中的数据写入文本文件中。
 
package com.xiao5_day_work;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 从控制台输入create user指令
 * 从控制台输入delete user指令
 * 从控制台输入update user指令
 * 从控制台输入list user指令
 * @author xiao
 *
 */

public class OperationTextTest {
    private OutputStream out;
    private BufferedWriter bos;
    private BufferedReader bis;

    // 实例化out;bos
    public void newoutput(String path) {
        try {
            out = new FileOutputStream(path, true);
            bos = new BufferedWriter(new OutputStreamWriter(out));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 删除最后的缓存文件
    public void deleteCacheFile(String cachePath) {
        File f = new File(cachePath);// 输入要删除的文件位置
        if (f.exists()) {
            f.delete();
        }
    }

    // 为了将缓存文件copy到目标文件
    public void copy(String path1, String path2) {
        try {
            InputStream in = new FileInputStream(path1);
            OutputStream out = new FileOutputStream(path2);
            byte[] buff = new byte[1024];
            int length = 0;
            while ((length = in.read(buff)) != -1) {
                out.write(buff, 0, length);
                out.flush();
            }
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 创建用户
    public void createUser() {
        System.out.println("请输入用户名:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        if (str != "") {
            str = "用户名:" + str + "\t";
            try {
                bos.write(str);
                bos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("请输入密码:");
        str = sc.nextLine();
        if (str != "") {
            str = "密码:" + str + "\t" + "\r\n";
            try {
                bos.write(str);
                bos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("添加成功!");
        }
    }

    // 删除用户
    public void deleteUser(String path) {
        System.out.println("请输入用户名:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        List<String> sList = new ArrayList<String>();
        String rstring;
        try {
            bis = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            while ((rstring = bis.readLine()) != null) {
                Pattern pattern = Pattern.compile(str);
                Matcher matcher = pattern.matcher(rstring);
                if (matcher.find()) {
                    System.out.println("找到!");
                    System.out.println("删除成功!");
                } else {
                    sList.add(rstring);
                }
            }
            OutputStream out1 = new FileOutputStream("f:\\delete.txt");// delete后创建的缓存文件
            PrintWriter printWriter1 = new PrintWriter(out1);
            for (String string : sList) {
                printWriter1.print(string);
                printWriter1.println();
                printWriter1.flush();
            }
            printWriter1.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        copy("f:\\delete.txt", path);
        deleteCacheFile("f:\\delete.txt");
    }

    // 更新用户数据
    public void updateUser(String path) {
        System.out.println("请输入用户名:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println("请输入密码:");
        String oldpassword = sc.nextLine();
        List<String> sListupdata = new ArrayList<String>();
        String rstring;
        try {
            bis = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            while ((rstring = bis.readLine()) != null) {
                Pattern pattern = Pattern.compile(str);
                Pattern pattern2 = Pattern.compile(oldpassword);
                Matcher matcher = pattern.matcher(rstring);
                Matcher matcher2 = pattern2.matcher(rstring);
                if (matcher.find() && matcher2.find() == true) {
                    System.out.println("该用户存在");
                    System.out.println("请输入新的密码");
                    String newpassword = sc.nextLine();
                    rstring = "用户名:" + str + "\t" + "密码:" + newpassword + "\t";
                    sListupdata.add(rstring);
                    System.out.println("修改成功!");
                } else {
                    sListupdata.add(rstring);
                }
            }
            OutputStream out2 = new FileOutputStream("f:\\update.txt");// update后创建的缓存文件
            PrintWriter printWriter2 = new PrintWriter(out2);
            for (String string : sListupdata) {
                printWriter2.print(string);
                printWriter2.println();
                printWriter2.flush();
            }
            printWriter2.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        copy("f:\\update.txt", path);
        deleteCacheFile("f:\\update.txt");
    }

    // 输出所有用户
    public void listUser(String path) {
        String rstring;
        try {
            bis = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            if ((rstring = bis.readLine()) == null) {
                System.out.println("没有用户!");
            }
            while ((rstring = bis.readLine()) != null) {
                int strindex = rstring.indexOf("密码:");
                int strend = rstring.lastIndexOf("\t");
                String passwordString = rstring.substring(strindex + 3, strend);
                String npstr = rstring.replace(passwordString, "******");
                System.out.println(npstr);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 退出程序
    public void quit() {
        System.out.println("退出成功!");
    }

    // 用户数添删查改等操作
    public void operationFile(String path) {
        try {
            newoutput(path);// out;bos实例化出来
            // 接收输入的数据
            Scanner sc = new Scanner(System.in);
            // 这里for(;;)性能高于while(true)
            for (;;) {
                String str = sc.nextLine();
                // create user
                if (str.equals("create user")) {
                    createUser();
                }
                // delete user
                if (str.equals("delete user")) {
                    deleteUser(path);
                }
                // update user
                if (str.equals("update user")) {
                    updateUser(path);
                }
                // list user
                if (str.equals("list user")) {
                    listUser(path);
                }
                if (str.equals("quit")) {
                    quit();
                    break;
                }
            }
            sc.close();
        } finally {
            deleteCacheFile("f:\\delete.txt");
            deleteCacheFile("f:\\update.txt");
        }
    }

    public static void main(String[] args) {
        new OperationTextTest().operationFile("f:\\NameAndPassword.txt");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值