■ 공부/JAVA

[JAVA] File 클래스 : cmd 구현하기

J U N E 2024. 1. 25. 16:27
public class Main {
    private static Scanner scanner = new Scanner(System.in);
    private static File cdir = new File("C:\\");

    public static void main(String[] args) {

        while (true) {
            System.out.print(cdir.getPath() + ">");
            String command = scanner.nextLine();
            if (command.compareToIgnoreCase("exit") == 0) break;
            String[] split = command.split("\\s");
            switch (split[0].toLowerCase()) {
                case "cd":
                    if (split.length > 1) command_cd(split[1]);
                    break;
                case "dir":
                    command_dir();
                    break;
                case "copy":
                    if (split.length > 2) command_copy(split[1], split[2]);
                    break;
                case "date":
                    command_date();
                    break;
                case "time":
                    command_time();
                    break;
                case "mkdir":
                    if (split.length > 1) command_mkdir(split[1]);
                    break;
                case "cls":
                    command_cls();
                    break;
                case "chdir":
                    command_chdir();
                    break;
                case "type":
                    if (split.length > 1)
                        command_type(split[1]);
                    break;
                default:
                    System.out.println("'" + split[0] + "'은(는) 알 수 없는 명령입니다.");
            }
        }
    }

    static int command_cd(String arg) {
        if (arg.equals("\\")) {
            cdir = new File("C:\\");
            return 0;
        }
        if (arg.equals("..")) {
            String parent = cdir.getParent();
            if (parent == null) {
                System.out.println("상위로 더이상 이동할 수 없습니다.");
                return -1;
            }
            cdir = new File(parent);
            return 0;
        }
        arg = arg.replace("/", "\\");
        File file = new File(cdir, arg);
        if (!file.exists()) {
            System.out.println("지정된 경로를 찾을 수 없습니다.");
            return -2;
        }
        cdir = file;
        return 0;
    }

    static int command_dir() {
        File[] fileList = cdir.listFiles();
        long used = 0, files = 0, dirs = 0;
        for (File file : fileList) {
            long lmt = file.lastModified();
            long fsize = file.length();
            String field;
            if (file.isDirectory()) {
                field = String.format("%-14s", "<DIR>");
                dirs++;
            }
            else {
                used += fsize;
                field = NumberFormat.getInstance().format(fsize);
                files++;
            }
            System.out.printf("%tY-%tm-%td %tp %tH:%tM   %14s %s\n",
                    lmt, lmt, lmt, lmt, lmt, lmt, field, file.getName());
        }
        System.out.printf("%15d개 파일 %18s 바이트\n", files, NumberFormat.getInstance().format(used));
        System.out.printf("%15d개 디렉토리 %15s 바이트 남음\n", dirs, NumberFormat.getInstance().format(cdir.getUsableSpace()));
        return 0;
    }

    static int command_copy(String arg1, String arg2) {
        File src = new File(cdir, arg1);
        File dst = new File(cdir, arg2);
        if (src.equals(dst)) {
            System.out.println("같은 파일로 복사할 수 없습니다.");
            return 0;
        }
        if (!src.exists()) {
            System.out.println("지정된 파일을 찾을 수 없습니다.");
            return -1;
        }
        if (src.isDirectory()) {
            System.out.println("디렉토리는 복사할 수 없습니다.");
            return -2;
        }
        if (dst.exists()) {
            System.out.println("'" + arg2 +"'을(를) 덮어쓰시겠습니까? (y/N): ");
            String answer = scanner.nextLine().toLowerCase();
            if (!answer.startsWith("y")) return 0;
        }
        try {
            FileReader fr = new FileReader(src);
            FileWriter fw = new FileWriter(dst);
            char[] cbuf = new char[4096];
            int n = cbuf.length;
            while (n >= cbuf.length) {
                n = fr.read(cbuf, 0, cbuf.length);
                fw.write(cbuf, 0, n);
            }
            fw.flush();
            fw.close();
            fr.close();
            System.out.println("파일이 복사되었습니다.");
        }
        catch (IOException e) {
            e.printStackTrace();
            return -3;
        }
        return 0;
    }

    static int command_date() {
        System.out.println("현재 날짜: " + new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
        return 0;
    }

    static int command_time() {
        System.out.println("현재 시간: " + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));
        return 0;
    }

    static int command_mkdir(String arg) {
        File file = new File(cdir, arg);
        if (file.exists()) {
            System.out.println("'" + arg + "'이(가) 이미 있습니다.");
            return -1;
        }
        try {
            file.createNewFile();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -2;
        }
        return 0;
    }

    static int command_cls() {
        for (int i = 0; i < 10; i++) System.out.println();
        return 0;
    }
    static int command_chdir() {
        System.out.println(cdir);
        return 0;
    }

    static int command_type(String arg) {
        File file = new File(cdir, arg);
        if (!file.exists()) {
            System.out.println("지정된 파일을 찾을 수 없습니다.");
            return -1;
        }
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            fr.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return -2;
        }
        return 0;
    }