자바
자바 객체 파일저장/로드 기본코드
by 가오가이거
2020. 12. 12.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
*
* 나열된 메소드를 적절히 구현 하세요.
* 적절한 예외처리를 해주세요.
*/
public class PhoneMgr {
/**
*
* @param count
* @param fileName
*
* //count 만큼 전화번호를 입력받는 기능을 작성하세요.. (IO 이용해서...)
//이름, 전화번호를 입력받아서
//입력받은 파일으름으로
// ex) 홍길동 : 010-222-2222
//파일에 저장!!
*/
public static void inputPhone(int count, String fileName) {
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new FileWriter(fileName, true))) {
for(int i = 0; i <count; i++) {
System.out.println("이름을 입력하세요. ");
String name = br.readLine();
System.out.println("전화번호를 입력하세요. ");
String phoneNumber = br.readLine();
pw.println(name +":"+phoneNumber);
}
}catch (Exception e) {
e.printStackTrace();
}
}
//매개변수로 입력받은 파일명에 해당하는 파일을 읽어서 출력하세요.
public static void printPhoneList(String fileName) {
try(BufferedReader br = new BufferedReader(new FileReader(fileName))){
String line = null;
while((line = br.readLine())!= null)
System.out.println(line);
}catch (Exception e) {
e.printStackTrace();
}
}
//파일을 읽어서, 읽은 내용으로 Phone객체를 생성하고, Phone객체를 List에 담아서 반환 하세요.
public static List<Phone> getPhoneList(String fileName) {
List<Phone> phoneList = new ArrayList<Phone>();
try(BufferedReader br = new BufferedReader(new FileReader(fileName))){
String line = null;
while((line = br.readLine())!= null) {
//이름:전화번호 -이 값으로 Phone 객체를 생성하고, list에 담는 코드를 수행.
String[] info = line.split(":");
Phone phone = new Phone();
phone.setName(info[0]);
phone.setPhoneNumber(info[1]);
phoneList.add(phone);
}
}catch (Exception e) {
e.printStackTrace();
}
return phoneList;
}
public static void main(String[] args) {
String fileName = "phone.txt";
/*
* inputPhone(3,fileName); printPhoneList(fileName);
*
* List<Phone> phoneList = getPhoneList(fileName);
* System.out.println(phoneList);
*/
inputPhoneObject(2, "phoneListObj.txt");
List<Phone> phoneList = getPhoneList2("phoneListObj.txt");
System.out.println(phoneList);
}
//입력받아서 리스트 객체로 저장
public static void inputPhoneObject(int count, String fileName) {
List<Phone> phoneList = new ArrayList<Phone>();
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ObjectOutputStream pw = new ObjectOutputStream(new FileOutputStream(fileName, true))) {
for(int i = 0; i <count; i++) {
System.out.println("이름을 입력하세요. ");
String name = br.readLine();
System.out.println("전화번호를 입력하세요. ");
String phoneNumber = br.readLine();
Phone phone = new Phone(name,phoneNumber);
phoneList.add(phone);
}
pw.writeObject(phoneList);
}catch (Exception e) {
e.printStackTrace();
}
}
//객체로 저장된 파일을 읽어서 리스트로 반환
public static List<Phone> getPhoneList2(String fileName) {
List<Phone> list = null;
try(
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
){
list = (List<Phone>)ois.readObject();
}catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
import java.io.Serializable;
public class Phone implements Serializable{
private String name;
private String phoneNumber;
public Phone() {}
public Phone(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "Phone [name=" + name + ", phoneNumber=" + phoneNumber + "]";
}
}