Java小项目——文件管理系统

项目要求:
Java小项目——文件管理系统_第1张图片

package kaoshi2;

import com.jw.file.FileTest4;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

import kaoshi.Doument;
import org.apache.commons.io.FileUtils;

public class DocumentTest {
    private static Scanner sc = new Scanner(System.in);
    private static List<User> users = new ArrayList<>();// 注册所有用户信息
    private static final String root = "C:\\Users\\15651\\Desktop\\user";// 存储所有用户资料的根目录
    private static User curUser = null;// 存储当前用户的登录信息

    static {
        try(ObjectInputStream input = new ObjectInputStream(new FileInputStream("users.txt"))){
            users = (List<User>)input.readObject();
        }catch (Exception e){
            users = new ArrayList<>();
        }
    }

    public static void main(String[] args) throws IOException {
        init();
    }
    public static void init() throws IOException {

        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("======欢迎来到个人资料管理系统=====");
            System.out.println("1:显示资料 "+ "\n" +"2:用户登录 "+ "\n" +"3:用户注册 "+ "\n" +"4:添加文件"+ "\n" +"5:删除文件" + "\n" +
                    "6:查看文件内容 "+ "\n" +"7:上传文件 "+ "\n" +"8:退出程序 ");
            System.out.println("请输入功能序号:");
            int num = sc.nextInt();
            if (num == 1||num==4||num==5||num==6||num==7){
                if (curUser==null){
                    System.out.println("===当前操作需要登陆!!===");
                    login();
                }
            }
            switch (num) {
                case 1:
                    showInfo();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    register();
                    break;
                case 4:
                    addFile();
                    break;
                case 5:
                    delete();
                    break;
                case 6:
                    showDetail();
                    break;
                case 7:
                    upload();
                    break;
                default:
                    System.out.println("输入的功能编号有误");
                    break;
            }
        }
    }
        // 上传文件
    private static void upload() throws IOException {
        System.out.println("请输入要上传的文件路径");
        String src = sc.next();
        String ext = src.substring(src.lastIndexOf("."),src.length());// 获取用户上传文件的后缀

        String fileName = FileUtil.getFileName()+ext;//5565565665.txt
        String destPath = root+"\\"+curUser.getUsername()+"\\"+fileName;
        File srcFile = new File(src);
        File destFile = new File(destPath);
//        try(
//                BufferedInputStream bufinput = new BufferedInputStream(new FileInputStream(srcFile));
//                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
//
//        ){
//            byte[] buf = new byte[1024];
//            int len;
//            while ((len=bufinput.read(buf))!=-1){
//                out.write(buf,0,len);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        FileUtils.copyFile(srcFile,destFile);
    }

    // 显示资料
    public static void showInfo(){
        System.out.println("===============显示个人资料信息================");
        File file = new File(root, curUser.getUsername());
        File[] files = file.listFiles();
        System.out.println("文件名     最后修改时间    文件类型");
        for(File f :files){
            //  修改时间f.lastModified()
            SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(f.getName()+"    "+format.format(f.lastModified())+"    "+(f.isDirectory()?"文件夹":"文件"));
        }

    }
    // 登陆
    public static void login(){
        System.out.println("================登录中心======================");
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        User user = new User(username,password);
        if(users.contains(user)){
            System.out.println("登录成功");
            curUser = user;
        }else{
            System.out.println("登录失败,请重新登录,如果还没有注册,是否要先注册?是请输入yes,否则no");
            String is = sc.next();
            if("yes".equals(is)){
                register();
            }else{
                login();
            }

        }
    }
    // 注册
    public static void register(){
        System.out.println("==================用户注册=============");
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        User user = new User(username, password);
        users.add(user);
        try  (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("users.txt"));){
           out.writeObject(users);
            System.out.println("注册成功");
            // 注册成功后创建个人 资料文件夹
            new File(root,username).mkdirs();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    // 添加文件
    public static void addFile(){
        System.out.println("========添加文件=======");
        System.out.println("1.创建文件夹  2.创建文件");
        System.out.println("请输入功能编号:");
        int num = sc.nextInt();
        switch (num){
            case 1:
                System.out.println("请输入文件夹名,如果是多级文件夹,\\隔开");
                String path = sc.nextLine();
                path = sc.nextLine();
                String parent = root+"\\"+curUser.getUsername();
                File file = new File(parent,path);
                file.mkdirs();
                break;
            case 2:
                while (true) {
                    System.out.println("请输入文件名");
                    String filename = sc.nextLine();
                    filename = sc.nextLine();
                    String parent2 = root + "\\" + curUser.getUsername();
                    File f = new File(parent2, filename);
                    if (f.isDirectory()) {
                        System.out.println("您创建的是文件夹,不是文件,请重新输入");
                    }else {
                        try {
                            f.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                }
        }

    }
    // 删除文件
    public static void delete(){
        System.out.println("===========删除文件============");
        File file = new File(root,curUser.getUsername());
        String[] list = file.list();
        System.out.println(Arrays.toString(list));
        System.out.println("请输入要删除的文件名");
        String fname = sc.next();
        File f = new File(root,curUser.getUsername()+"\\"+fname);
        boolean flag = f.delete();
        if (flag){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");

        }


    }
    // 显示文件详情
    public static void showDetail(){
        System.out.println("请输入想要查看文件信息的文件名");
        String name = sc.next();
        String path = root+"\\"+curUser.getUsername()+"\\"+name;
        File f = new File(path);
        try(BufferedInputStream bufinput = new BufferedInputStream(new FileInputStream(f))){
            byte[] buf = new byte[1024];
            int len;
            while ((len = bufinput.read(buf))!=-1){
                System.out.println(new String(buf));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
class User implements Serializable {
    private String username;
    private String password;
    public User() {}
    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;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return username.equals(user.username) &&
                password.equals(user.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, password);
    }
}



你可能感兴趣的:(java,开发语言,算法)