๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Java] Lecture - Day 18 ๋ณธ๋ฌธ

Development Study/Java

[Java] Lecture - Day 18

f1r3_r41n 2024. 6. 6. 14:50
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ Basic Java - Day 18

๐Ÿ‘‰๐Ÿป [Chapter 13] ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ๊ณผ ํŒŒ์ผ ์ž…์ถœ๋ ฅ

ํŒŒ์ผ ํƒ์ƒ‰๊ธฐ: FileExplorer.java

  • ๋ช…๋ น์–ด
    • ํŒŒ์ผ ๋ชฉ๋ก ๋ณด๊ธฐ: dir
    • ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™: cd ..
    • ๋ถ€๋ชจ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™: cd ๋””๋ ‰ํ† ๋ฆฌ๋ช…
    • ๋””๋ ‰ํ† ๋ฆฌ ๋งŒ๋“ค๊ธฐ: mkdir ๋””๋ ‰ํ† ๋ฆฌ๋ช…
    • ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ: rename {Old ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…} {New ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…}
    • ์‚ญ์ œ: del {ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…}
      • ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ๋‚˜ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋น„์–ด์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋งŒ ์‚ญ์ œํ•จ
    • ํŒŒ์ผ ๋ณต์‚ฌ: copy srcํŒŒ์ผ๋ช… destํŒŒ์ผ๋ช…
      • ๋””๋ ‰ํ† ๋ฆฌ๋Š” ๋ณต์‚ฌํ•  ์ˆ˜ ์—†์Œ
    • ์ข…๋ฃŒ: exit
  • ์ œ์•ฝ์‚ฌํ•ญ
    • ๊ณต๋ฐฑ์ด ์žˆ๋Š” ํŒŒ์ผ๋ช… ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
  • ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„๋“ค์ž„
    • ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ถ„์„ํ•˜์—ฌ ๋ช…๋ น์–ด๋ฅผ ์•Œ์•„๋ƒ„
    • ๋ช…๋ น์–ด์— ๋”ฐ๋ผ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
  • ๋ฌดํ•œ ๋ฃจํ”„๋Š” exit๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ๋จ
package explorer;

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

public class FileExplorer {
    File current = new File("C:\\");
    public FileExplorer() { }
    public void start() {
        Scanner sc = new Scanner(System.in);

        System.out.println("<< ํŒŒ์ผ ํƒ์ƒ‰๊ธฐ ๋ช…๋ น์–ด >>");
        System.out.println("1. ํŒŒ์ผ ๋ชฉ๋ก ๋ณด๊ธฐ:        dir");
        System.out.println("2. ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™:   cd ๋””๋ ‰ํ† ๋ฆฌ๋ช…");
        System.out.println("3. ๋ถ€๋ชจ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™:   cd ..");
        System.out.println("4. ๋””๋ ‰ํ† ๋ฆฌ ๋งŒ๋“ค๊ธฐ:        mkdir ๋””๋ ‰ํ† ๋ฆฌ๋ช…");
        System.out.println("5. ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ:           rename {Old ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…} {New ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…}");
        System.out.println("6. ์‚ญ์ œ:                 del ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ช…");
        System.out.println("7. ํŒŒ์ผ ๋ณต์‚ฌ:             copy srcํŒŒ์ผ๋ช… destํŒŒ์ผ๋ช…");
        System.out.println("8. ์ข…๋ฃŒ:                 exit\n");

        while(true) {
            System.out.print("\n"+current.getPath()+"> ");
            String input = sc.nextLine();
            StringTokenizer st = new StringTokenizer(input);
            boolean chk = false;
            try {
                switch(st.nextToken()) {
                    case "dir" -> list();
                    case "cd" -> changeDir(st.nextToken());
                    case "mkdir" -> makeDir(st.nextToken());
                    case "rename" -> rename(st.nextToken(), st.nextToken());
                    case "del" -> delete(st.nextToken());
                    case "copy" -> copy(st.nextToken(), st.nextToken());
                    case "exit" -> chk = true;
                    default -> System.out.println("์ž˜๋ชป๋œ ๋ช…๋ น์–ด ํ˜•์‹์ž…๋‹ˆ๋‹ค.");
                }
            } catch(NoSuchElementException e) {
                System.out.println("๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ• ์˜ค๋ฅ˜...");
                e.printStackTrace();
            }
            if(chk) break;
        }
        sc.close();
    }

    public void list() {
        File[] l = current.listFiles();
        if(l==null||l.length==0) {
            System.out.println("No files and No directories");
            return;
        }
        for(File t:l) {
            Date lM = new Date(t.lastModified());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy๋…„ MM์›” dd์ผ HH:mm:ss");
            System.out.println(sdf.format(lM)+"\t");
            if(t.isDirectory()) System.out.printf("<DIR>%24s\t", "");
            else {
                DecimalFormat docFormat = new DecimalFormat("###,###");
                System.out.printf("%24s     \t", docFormat.format(t.length()));
            }
            System.out.println(t.getName());
        }
    }

    public void changeDir(String dir) {
        if(dir.equals("..")) {
            changeToParent();
            return;
        }
        File f = new File(current.getPath(), dir);
        if(f.exists()&&f.isDirectory()) current = f;
        else System.out.println(dir+" ๋””๋ ‰ํ† ๋ฆฌ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
    }

    public void changeToParent() {
        if(current.getPath().equals("C:\\")) {
            System.out.println("C:\\๋Š” ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค.");
            return;
        }
        current = new File(current.getParent());
    }

    public void makeDir(String dir) {
        File f = new File(current.getPath(), dir);
        if(!f.exists()) f.mkdir();
        else System.out.println(dir+"๋Š”(์€) ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค.");
    }

    public void rename(String src, String dest) {
        File srcf = new File(current.getPath(), src);
        File destf = new File(current.getPath(), dest);
        if(!srcf.exists()) {
            System.out.println(src+"๋Š”(์€) ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
            return;
        } else if(destf.exists()) {
            System.out.println(dest+"๋Š”(์€) ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.");
            return;
        }
        srcf.renameTo(destf);
    }

    public void delete(String name) {
        File f = new File(current.getPath(), name);
        if(f.exists()) f.delete();
        else System.out.println(name+"๋Š”(์€) ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
    }

    public void copy(String src, String dest) {
        File srcf = new File(current.getPath(), src);
        File destf = new File(current.getPath(), dest);
        if(!srcf.exists()) {
            System.out.println(src+"๋Š”(์€) ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
            return;
        } else if(destf.exists()) {
            System.out.println(dest+"๋Š”(์€) ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.");
            return;
        } else if(srcf.isDirectory()) {
            System.out.println(src+"๋Š”(์€) ๋””๋ ‰ํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค.");
            return;
        }
        BlockBinaryCopy bbc = new BlockBinaryCopy();
        bbc.copy(srcf, destf);
    }

    public static void main(String[] args) {
        FileExplorer exp = new FileExplorer();
        exp.start();
    }
}
  • FileExplorer.java
package explorer;

import java.io.*;

public class BlockBinaryCopy {
    public BlockBinaryCopy() { }
    public void copy(File src, File dest) {
        try {
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(dest);
            byte[] buf = new byte[1024*10];
            while(true) {
                int n = fis.read(buf);
                fos.write(buf, 0, n);
                if(n<buf.length) break;
            }
            fis.close();
            fos.close();
            System.out.println(
                    src.getPath() + "๋ฅผ "
                            + dest.getPath() + "๋กœ ๋ณต์‚ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค."
            );
        } catch(IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
}
  • BlockBinaryCopy.java

Data, SimpleDateFormat ํด๋ž˜์Šค

  • ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค
    • Date ํด๋ž˜์Šค
      • ๊ธฐ๋Šฅ ๋นˆ์•ฝ
      • java.util.Date
      • Date today = new Date()
    • Calendar ํด๋ž˜์Šค
      • Date ํด๋ž˜์Šค๋ณด๋‹ค๋Š” ๊ธฐ๋Šฅ์ด ๋งŽ์ง€๋งŒ ๋‹จ์ ๋“ค์ด ์กด์žฌ
      • java.util.Calendar
      • Calendar today = Calendar.getInstance()
    • java.time ํŒจํ‚ค์ง€
  • SimpleDateFormat
    • ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋‹ค์–‘ํ•˜๊ฒŒ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋‹ค์–‘ํ•œ ํฌ๋งท์„ ์ง€์›
    • java.text.SimpleDateFormat
Date td = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String re = sdf.format(td);
System.out.println(re);

SimpleDateFormat sdf = new SimpleDateFormat("yy๋…„ M์›” d์ผ E์š”์ผ, H์‹œ m๋ถ„ s์ดˆ, a");
String re = sdf.format(td);
System.out.println(re);

long time = td.getTime(); // 1970๋…„ 1์›” 1์ผ 0์‹œ:0๋ถ„:0์ดˆ ๋ถ€ํ„ฐ millisecond๋กœ ์‹œ๊ฐ„ ์ €์žฅ
System.out.println(sdf.format(time));

DecimalFormat ํด๋ž˜์Šค

  • ์ˆซ์ž ๋ฐ์ดํ„ฐ์— ์„œ์‹ํ™”๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
  • ์ˆซ์ž ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ํฌ๋งท์„ ์ œ๊ณตํ•จ
  • java.text.DecimalFormat
DecimalFormat df = new DecimalFormat("###,###");  // #: 10์ง„์ˆ˜  ,: ๋‹จ์œ„ ๊ตฌ๋ถ„์ž
DecimalFormat df2 = new DecimalFormat("000.00");  //.: ์†Œ์ˆ˜์  0: ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ 0
DecimalFormat df3 = new DecimalFormat("#.#-");  // -: ์Œ์ˆ˜๋ถ€ํ˜ธ
DecimalFormat df4 = new DecimalFormat("#.#%");  // %: ํผ์„ผํŠธ

String re1 = df.format(100000000);  // 100,000,000
String re2 = df2.format(49.5);  // 049.50

System.out.println(re1);
System.out.println(re2);

System.out.println(df3.format(314.43)); // 314.43-
System.out.println(df4.format(0.739));  // 73.9%
728x90
๋ฐ˜์‘ํ˜•

'Development Study > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] Lecture - Day 19  (0) 2024.06.07
[Java] Lecture - Day 17  (1) 2024.06.06
[Java] Lecture - Day 16  (1) 2024.05.17
[Java] Lecture - Day 15  (0) 2024.04.26
[Java] Lecture - Day 14  (0) 2024.04.05
Comments