使用IO流的综合练习

  • 在学习了IO流的相关知识之后,我们需要一个综合性较高的项目巩固一下IO流的知识

  • 今天给大家带来一个相关练习

  • 需求:

    • 创建一个——个人资料管理系统
    • 相关的功能要求:
      1. 显示资料
      2. 用户登录
      3. 用户注册(要求运用序列化相关知识完成将注册用户信息持久保存)
      4. 添加文件
      5. 删除文件
      6. 查看文件
      7. 上传文件
      8. 移动文件(仅一级目录就好)
  • 给大家分享一下我所设计的程序,些许功能不完善,感兴趣的小伙伴可以一起交流

  • 根据创建文件起名字代码:

import java.util.Date;

public class FileName {
    public static String getFileName() {
        return new Date().getTime()+"";
    }

}

  • 用户代码:
public class User {
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

  • 资料管理相关代码
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Managing {
    private String name;
    private static Map<String,String> map = new HashMap<>();
    final String PATH = "F:\\java练习\\data\\";
    private String currentuser;
    Scanner sc = new Scanner(System.in);

    static {
        try (ObjectInputStream objInput = new ObjectInputStream(new FileInputStream("user.txt"))){
            map = (Map<String,String>)objInput.readObject();
        }  catch (Exception e) {
            map = new HashMap<String,String>();
        }
    }
    public Managing() {

    }

    public Managing(String name) {
        this.name = name;
        Map<String,String> map = new HashMap<>();
    }

    public String getCurrentuser() {
        return currentuser;
    }

    public void setCurrentuser(String currentuser) {
        this.currentuser = currentuser;
    }

    public void menu() {
        System.out.println("==========欢迎来到【"+name+"】个人资料管理系统==========");
        System.out.print("1.显示资料");
        System.out.print(" 2.用户登录");
        System.out.print(" 3.用户注册");
        System.out.println(" 4.添加文件");
        System.out.print("5.删除文件");
        System.out.print(" 6.查看文件");
        System.out.print(" 7.上传文件");
        System.out.println(" 8.移动文件");
    }

    public void show() {
        System.out.println("========== 显示资料 ==========");
        File file = new File(PATH,currentuser);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        File[] files = file.listFiles();
        System.out.println("文件名         最后修改时间      文件类型");
        for (File f:files) {
            System.out.println(f.getName()+"        "+format.format(f.lastModified())+"         "+(f.isDirectory()?"文件夹":"文件"));
        }
    }

    public void login() {
        System.out.println("========== 用户登录 ==========");
            if (currentuser!=null) {
                System.out.println("抱歉,当前系统已经有账号在线了~");
                return;
            }
            System.out.println("请输入您的用户名:");
            String username = sc.next();
            System.out.println("请输入您的密码:");
            String password = sc.next();
            String passwordValue = map.get(username);
            if (!passwordValue.equals(password) && username==null) {
                System.out.println("抱歉,您的用户名或者密码有误,请重新输入:");
                login();
            }
            currentuser = username;
            System.out.println("登录成功!");
            return;
        }

    public void register() {
            System.out.println("========== 用户注册 ==========");
            System.out.println("请输入您想要注册的用户名:");
            String username = sc.next();
            Set<String> keys = map.keySet();
            for (String key:keys) {
                if (key.equals(username)) {
                    System.out.println("您输入的用户名已存在,请重新输入:");
                    register();
                }
            }
            System.out.println("请输入您想要注册的密码:");
            String password = sc.next();
            System.out.println("请再次输入您想要注册的密码");
            String password1 = sc.next();
            if (!password.equals(password1)) {
                System.out.println("抱歉,您输入的两次密码不一致,请重新输入:");
                register();
            }
            map.put(username,password);
            File file = new File(PATH, username);
            file.mkdirs();
            try (ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("user.txt"))){
                objOut.writeObject(map);
                System.out.println("注册成功!");
            } catch (Exception e) {
                e.printStackTrace();
        }
    }

    public void add() {
        System.out.println("========== 添加文件 ==========");
        System.out.println("请问您想创建新的文件夹还是文件? 1:文件夹 2:文件");
        int num = sc.nextInt();
        switch (num) {
            case 1:
                System.out.println("请输入文件夹的名字,如果想创建多级文件夹,请输入例如:a//b");
                String name = sc.nextLine();
                name = sc.nextLine();
                File file = new File(PATH + currentuser, name);
                file.mkdirs();
                System.out.println("添加成功!");
                break;
            case 2:
                while (true) {
                    System.out.println("请输入文件的名字:");
                    String fileName = sc.nextLine();
                    fileName = sc.nextLine();
                    File file1 = new File(PATH + currentuser, fileName);
                    if (file1.isDirectory()) {
                        System.out.println("您输入的是文件夹不是文件,请重新创建");
                    } else {
                        try {
                            file1.createNewFile();
                            System.out.println("创建成功~");
                            return;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            default:
                System.out.println("您输入的数字有误,请重新输入:");
                add();
                break;
        }
    }

    public void del() {
        System.out.println("========== 删除文件 ==========");
        show();
        System.out.println("请输入你想要删除的文件名");
        String fileName = sc.next();
        File file1 = new File(PATH + currentuser, fileName);
        boolean flag = file1.delete();
        if (flag) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    public void check() {
        System.out.println("========== 查看文件 ==========");
        show();
        System.out.println("请输入您要查看的文件名字:");
        String fileName = sc.next();
        File file = new File(PATH + currentuser, fileName);
        try (BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
        ){
        int len;
        byte[] b = new byte[1024];
        while ((len=bufInput.read(b))!=-1) {
            System.out.println("您要查看的文件内容为:"+new String(b,0,len));
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void uploadFile() {
        System.out.println("========== 上传文件 ==========");
        System.out.println("请输入您想要上传的文件路径:");
        String src = sc.next();
        String suffix = src.substring(src.lastIndexOf("."), src.length());
        String fileName = FileName.getFileName()+suffix;
        File srcFile = new File(src);
        File destFile = new File(PATH+currentuser,fileName);
        try (BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(destFile))){
            int len;
            byte[] b = new byte[1024];
            while ((len=bufInput.read(b))!=-1) {
                bufOut.write(b,0,len);
            }
            System.out.println("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
    }

    public void move() {
        System.out.println("========== 移动文件 ==========");
        System.out.println("请问您要移动的是:1:文件夹 2.文件");
        int num = sc.nextInt();
        if (num==1) {
            show();
            System.out.println("请输入您想要移动的文件夹名");
            String dirName1 = sc.next();
            File src = new File(PATH + currentuser, dirName1);
            if (src==null && src.exists() && src.isDirectory()) {
                System.out.println("请输入您想要将该文件夹移动到哪一个文件夹下面(在文件夹前加//):");
                String dirName = sc.next();
                File dest = new File(PATH + currentuser + dirName, dirName1);
                try {
                    FileUtils.copyDirectoryToDirectory(src, dest);
                    //src.delete();
                    System.out.println("移动成功");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("移动失败");
                }
            }
        }
        else if (num==2) {
            show();
            System.out.println("请输入您想要移动的文件名");
            String fileName = sc.next();
            File src = new File(PATH + currentuser, fileName);
            if (src.exists()) {
                System.out.println("请输入您想要将该文件移动到哪一个文件夹下面(在文件夹前加//):");
                String dirName = sc.next();
                File dest = new File(PATH + currentuser + dirName, fileName);
                try {
                    FileUtils.copyFile(src, dest);
                    src.delete();
                    System.out.println("移动成功");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("移动失败");
                }
            }
        }
    }
}

  • 测试类代码:
import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        Managing managing = new Managing("Apex");
        while (true) {
            managing.menu();
            System.out.println("请输入你想要操作的功能编号:");
            Integer num = sc.nextInt();
            if (num==1 || num==4 || num==5 || num==6 || num==7|| num==8) {
                if (managing.getCurrentuser()==null) {
                    System.out.println("想要进行此操作需要先进行登录");
                    managing.login();
                }
            }

            switch (num) {
                case 1:
                    managing.show();
                    break;
                case 2:
                    managing.login();
                    break;
                case 3:
                    managing.register();
                    break;
                case 4:
                    managing.add();
                    break;
                case 5:
                    managing.del();
                    break;
                case 6:
                    managing.check();
                    break;
                case 7:
                    managing.uploadFile();
                    break;
                case 8:
                    managing.move();
                    break;
                default:
                    System.out.println("你输入的编号有误~请重新输入:");
                    break;
            }
        }
    }
}

你可能感兴趣的:(JavaSE,java,开发语言,IO流)