※ com.my.product.dao.ProductDAOFile 작성하기
ex) 파일명 : D:\products.txt
파일구조
#상품번호값:상품명값:상품가격값
C0001:아메리카노:1000
C0002:아이스아메리카노:1000
package com.my.product.dao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.my.exception.AddException;
import com.my.exception.FindException;
import com.my.exception.ModifyException;
import com.my.exception.RemoveException;
import com.my.product.dto.Product;
public class ProductDAOFile implements ProductDAOInterface {
private String fileName = "D:\\products.txt";
public ProductDAOFile() {
createFile();
}
private void createFile() {
File file = new File(fileName);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void insert(Product product) throws AddException {
try {
selectByProdNo(product.getProdNo());
throw new AddException("이미 존재하는 상품입니다");
} catch (FindException e) {
// e.printStackTrace();
FileWriter fw = null;
try {
fw = new FileWriter(fileName, true); // 기존 파일 끝에 append
String prodStr = product.getProdNo() + ":" + product.getProdName() + ":" + product.getProdPrice() + "\n";
fw.write(prodStr);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e1) {
}
}
}
}
}
@Override
public Product selectByProdNo(String no) throws FindException {
Scanner sc = null;
try {
sc = new Scanner(new FileInputStream(fileName));
while(sc.hasNextLine()) {
String line = sc.nextLine(); // 한줄로 읽어옴
String[] arr = line.split(":");
String prodNo = arr[0];
if(prodNo.equals(no) ) {
return new Product(prodNo, arr[1], Integer.parseInt(arr[2]));
}
}
throw new FindException("번호에 해당하는 상품이 없습니다");
} catch (FileNotFoundException e) {
e.printStackTrace();
createFile();
throw new FindException("번호에 해당하는 상품이 없습니다");
} finally {
if(sc !=null) {
sc.close();
}
}
}
@Override
public Object selectAll() throws FindException {
List<Product> all = new ArrayList<>();
Scanner sc = null;
try {
// Scanner sc = new Scanner(new File(fileName)); // 이거 써도 됨
sc = new Scanner(new FileInputStream(fileName));
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] arr = line.split(":");
String prodNo = arr[0];
String prodName = arr[1];
int prodPrice = Integer.parseInt(arr[2]);
Product p = new Product(prodNo, prodName, prodPrice);
all.add(p);
}
if(all.size() == 0) {
throw new FindException("상품이 하나도 없습니다");
}
return all;
} catch (FileNotFoundException e) {
e.printStackTrace();
createFile();
throw new FindException("상품이 하나도 없습니다");
} finally {
if(sc != null) {
sc.close();
}
}
}
@Override
public void update(Product p) throws ModifyException {
FileWriter fw = null;
try {
List<Product> all = (List)selectAll();
boolean updated = false;
for(Product savedP : all) {
if(savedP.equals(p)) { // if(savedP.getProdNo().equals(p.getProdNo())); {
if(p.getProdName() != null) {
savedP.setProdName(p.getProdName());
updated = true;
}
if(p.getProdPrice() != 0) {
savedP.setProdPrice(p.getProdPrice());
updated = true;
}
break;
}
}
if(updated) {
fw = new FileWriter(fileName);
for(Product savedP : all) {
String pStr = savedP.getProdNo() + ":" + savedP.getProdName() + ":" + savedP.getProdPrice() + "\n";
fw.write(pStr);
}
}
} catch (FindException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void delete(String prodNo) throws RemoveException {
FileWriter fw = null;
try {
List<Product> all = (List)selectAll();
/* 이렇게 써도 됨
Product savedP = new Product();
savedP.setProdNo(prodNo);
all.remove(saveP);
*/
fw = new FileWriter(fileName); // fileName, true 이거 아님
// fw = new FileWriter(fileName, false); // 가능
for(Product p : all) {
if(!p.getProdNo().equals(prodNo)) {
String pStr = p.getProdNo() + ":" + p.getProdName() + ":" + p.getProdPrice() + "\n";
fw.write(pStr);
}
}
} catch (FindException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
'언어 > Java' 카테고리의 다른 글
성능 향상 스트림 (0) | 2023.08.15 |
---|---|
기본자료형과 참조형의 차이 (0) | 2023.08.10 |
필터스트림, 직렬화 예시 (0) | 2023.08.09 |
필터스트림, 객체직렬화 (0) | 2023.08.09 |
메타클래스, File 정보 가져오기 (0) | 2023.08.09 |