์ผ์ ์ 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();
}
}
'๐๏ธ Backend > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ(Java) - JDBC(Oracle) : EMP ํ ์ด๋ธ์ Java์ ์ฐ๊ฒฐํ์ฌ ํฐ๋ฏธ๋์์ ์กฐํํ๊ธฐ (1) (0) | 2023.02.27 |
---|---|
์๋ฐ(Java) - JDBC(Oracle) : ์ด๋ก (?) (0) | 2023.02.27 |
์๋ฐ(Java) - ์ ์ถ๋ ฅ ์คํธ๋ฆผ ์ค์ต ๋ฌธ์ (0) | 2023.02.10 |
์๋ฐ(Java) - ์ ์ถ๋ ฅ ์คํธ๋ฆผ (0) | 2023.02.09 |
์๋ฐ(Java) - ์คํธ๋ฆผ(Stream) (0) | 2023.02.08 |