GiantStepDEV

์ผ์ „์— Map์„ ์ด์šฉํ•ด์„œ ์นดํŽ˜ ๋ฉ”๋‰ด ๋งŒ๋“ค๊ธฐ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์—ˆ์—ˆ๋Š”๋ฐ,

ํ•œ ๋‹จ๊ณ„ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•˜์—ฌ ์™ธ๋ถ€ ํŒŒ์ผ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ์ถ”๊ฐ€(๊ฐ์ฒด์ง๋ ฌํ™”)

๐Ÿ’ก ๊ฐ์ฒด ์ง๋ ฌํ™”(Serializable) ํ•˜๋Š” ์ด์œ ?

  • ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋””์Šคํฌ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ
    ๐Ÿถ ๋””์Šคํฌ์— ์ €์žฅ/๋„คํŠธ์›Œํฌ ํ†ต์‹ ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด ์ฐธ์กฐํƒ€์ž…์€ ์•ˆ๋˜๋Š”๊ฑด๊ฐ€?
  • ๋””์Šคํฌ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ฆ‰, ๊ฐ’ ํ˜•์‹ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€๋Šฅํ•จ.
    ๐Ÿถ ์•„, ์ฐธ์กฐ ํƒ€์ž… ๋ฐ์ดํ„ฐ๋Š” ๊ฐ’์ด ์•„๋‹Œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ๋˜๋Š”๊ฑฐ๊ตฌ๋‚˜.
    ๐Ÿธ ๋งž์•„.
  • ์ด๋Ÿฌํ•œ ์ฐธ์กฐ ํƒ€์ž… ๋ฐ์ดํ„ฐ(๊ฐ์ฒด)๋ฅผ ์ง๋ ฌํ™” ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ’ ํ˜•์‹ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ด์คŒ.

๐Ÿ’ก ํ•œ ์ค„ ์ •๋ฆฌ!!

์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ํŒŒ์ผ ์ €์žฅ ํ˜น์€๋„คํŠธ์›Œํฌ/๋ฐ์ดํ„ฐ ํ†ต์‹ ์—์„œ parsing ํ•˜์—ฌ ์œ ์˜๋ฏธํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ!

์‹ค์Šต์˜ˆ์ œ. ์นดํŽ˜ ๋ฉ”๋‰ด ๋งŒ๋“ค๊ธฐ

์ง๋ ฌํ™”

import java.io.Serializable;
public class CoffeeMenuInfo implements Serializable {
    private String name;
    private int price;
    private String group;
    private String desc;

    public CoffeeMenuInfo(String name, int price, String group, String desc) {
        this.name = name;
        this.price = price;
        this.group = group;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

SerialMenuWrite

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class SerialMenuWrite {
    static Map<String, CoffeeMenuInfo> map = new HashMap<>();
    public static void main(String[] args) throws IOException {
        menuWrite();
        selectMenu();
    }
    static void menuWrite() {
        map.put("Americano", new CoffeeMenuInfo("Americano", 2500, "Coffee", "๊ทธ๋ƒฅ ์ปคํ”ผ"));
        map.put("Espresso", new CoffeeMenuInfo("Espresso", 2500, "Coffee", "์ง„ํ•œ ์ปคํ”ผ"));
        map.put("Latte", new CoffeeMenuInfo("Latte", 4500, "Coffee", "์šฐ์œ ๊ฐ€ ๋“ค์–ด ์žˆ์–ด์š”."));
    }
    static void selectMenu() throws IOException {
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("๋ฉ”๋‰ด๋ฅผ ์„ ํƒ ํ•˜์„ธ์š” : ");
            System.out.print("[1]๋ฉ”๋‰ด ๋ณด๊ธฐ, [2]๋ฉ”๋‰ด ์ถ”๊ฐ€, [3]์ข…๋ฃŒ : ");
            int menu = sc.nextInt();
            switch (menu) {
                case 1 :
                    System.out.println("======= ๋ฉ”๋‰ด ๋ณด๊ธฐ =======");
                    for(String e : map.keySet()) {
                        System.out.println("๋ฉ”๋‰ด : " + map.get(e).getName());
                        System.out.println("๊ฐ€๊ฒฉ : " + map.get(e).getPrice());
                        System.out.println("๋ถ„๋ฅ˜ : " + map.get(e).getGroup());
                        System.out.println("์„ค๋ช… : " + map.get(e).getDesc());
                        System.out.println("------------------------------------");
                    }
                    break;
                case 2 :
                    System.out.print("์ถ”๊ฐ€ ํ•  ๋ฉ”๋‰ด๋ฅผ ์ž…๋ ฅ ํ•˜์„ธ์š” : ");
                    String key = sc.next();
                    if(map.containsKey(key)) {
                        System.out.println("ํ•ด๋‹น ๋ฉ”๋‰ด๊ฐ€ ์ด๋ฏธ ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค.");
                    } else {
                        System.out.print("๊ฐ€๊ฒฉ : ");
                        int price = sc.nextInt();
                        System.out.print("๋ถ„๋ฅ˜ : ");
                        String grp = sc.next();
                        sc.nextLine(); // ๋ฒ„ํผ ๋น„์šฐ๊ธฐ
                        System.out.print("์„ค๋ช… : ");
                        String desc = sc.nextLine();
                        map.put(key, new CoffeeMenuInfo(key, price, grp, desc));
                    }
                    break;
                case 3 :
                    System.out.println("๋ฉ”๋‰ด๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์ €์žฅ ํ•ฉ๋‹ˆ๋‹ค.");
                    FileOutputStream fos = new FileOutputStream("D:/์ง๋ ฌํ™”ํŒŒ์ผ/์ปคํ”ผ๋ฉ”๋‰ด.bin");
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    oos.writeObject(map); // ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ™”ํ•ด์„œ ํŒŒ์ผ์— ์ €์žฅ
                    oos.flush();
                    oos.close();
                    return;
            }

        }

    }
}

 

 

SerialMenuRead

import ์ง๋ ฌํ™”์—ฐ์Šต2๋ฒˆ.CoffeeMenuInfo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Map;

public class SerialMenuRead {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        getMenuList();
    }
    static void getMenuList() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("C:/Dev/work_java/Java_Big_Data_am/src/์ง๋ ฌํ™”์—ฐ์Šต3๋ฒˆ/์ปคํ”ผ๋ฉ”๋‰ด.bin");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Map<String, CoffeeMenuInfo> map = (Map<String, CoffeeMenuInfo>) ois.readObject();
        for(String e : map.keySet()) {
            System.out.println("๋ฉ”๋‰ด : " + map.get(e).getName());
            System.out.println("๊ฐ€๊ฒฉ : " + map.get(e).getPrice());
            System.out.println("๋ถ„๋ฅ˜ : " + map.get(e).getGroup());
            System.out.println("์„ค๋ช… : " + map.get(e).getDesc());
            System.out.println("------------------------------------");
        }
        fis.close();
        ois.close();
    }
}
profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!