(사진출처)https://gongu.copyright.or.kr/gongu/wrt/wrt/view.do?wrtSn=13049265&menuNo=200025
C.R.U.D.를 File 형태로
Export/Import 하기..
이번에 포스팅 할 프로젝트는 지난 포스팅인
2018/05/26 - [Java/Project] - C.R.U.D. 프로그램 만들기
를 version up 하여 만든 프로그램으로
콘솔을 종료할때 데이터의 기록을 파일화 시켜 export/import
하는 기능을 추가하였습니다.
mainClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package student; import java.util.Scanner; import dao.daoClass; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 인스턴스 생성 daoClass dao = new daoClass(); // 변수선언 int chNum; String ExImport; // init ExImport = ""; while (true) { System.out.println(); // 개행 System.out.println("# 실행 하실 작업의 번호를 입력하시오"); System.out.println(" 1. Insert - 추가 "); System.out.println(" 2. Delete - 삭제 "); System.out.println(" 3. Search - 검색 "); System.out.println(" 4. Update - 수정 "); System.out.print(" 5. Exit - 프로그램 종료\n>>>"); chNum = sc.nextInt(); switch (chNum) { case 1: // 1. Insert - 추가 dao.insert(); break; case 2: // 2. Delete - 삭제 dao.delete(); break; case 3: // 3. Search - 검색 dao.search(); break; case 4: // 4. Update - 수정 dao.update(); break; default: // 5. Exit - 프로그램 종료 System.out.print("\n파일을 저장하시겠습니까?(y/n)\n>>>"); ExImport = sc.next(); if(ExImport.equals("y")||ExImport.equals("Y")) { dao.exportData(); } System.out.println("\n프로그램을 종료합니다"); System.exit(0); } System.out.println(); } } } | cs |
daoClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | package dao; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import dto.dtoClass; // Data Access object (책) public class daoClass { Scanner sc = new Scanner(System.in); dtoClass dtoArr[] = new dtoClass[20]; public String export[] = new String[20]; // 변수선언 private boolean swit[] = new boolean[20]; private int pageNum; private String check; // test public void dataPrint() { for (int i = 0; i < dtoArr.length; i++) { if (dtoArr[i] != null) { System.out.println(dtoArr[i].toString()); } } } // init in mainProcess public daoClass() { init(); importData(); } // init public void init() { for (int i = 0; i < swit.length; i++) { swit[i] = false; } } // Insert - 추가 public void insert() { System.out.println(); // 개행 for (int i = 0; i < dtoArr.length; i++) { if (swit[dtoArr.length - 1] == true) { System.out.println("더 이상 페이지를 추가 할 수 없습니다"); return; } else if (swit[i] == false) { System.out.println((i + 1) + " 번 페이지에 작성 하실 수 있습니다.\n"); break; } } System.out.print("# 나이\n>>>"); int age = sc.nextInt(); System.out.print("# 이름\n>>>"); String name = sc.next(); System.out.print("# 생년월일\n>>>"); String birth = sc.next(); System.out.print("# 주소\n>>>"); String address = sc.next(); for (int i = 0; i < dtoArr.length; i++) { if (swit[i] == false) { swit[i] = true; pageNum = i + 1; dtoArr[i] = new dtoClass(age, name, birth, address, pageNum); System.out.println("\n" + pageNum + " 번째 페이지가 작성되었습니다"); export[i] = pageNum + "-" + age + "-" + name + "-" + birth + "-" + address; // System.out.println("daoClass.export[" + i + "] = " + export[i]); // 테스트 break; } } } // Delete - 삭제 public void delete() { System.out.print("\n삭제하고 싶은 책의 쪽수를 입력하시오(1 ~ 20)\n>>>"); pageNum = sc.nextInt(); if (dtoArr[pageNum - 1] == null) { System.out.println("\n작성된 페이지가 없는 쪽수입니다"); return; } System.out.println("\n삭제하고 싶은 " + pageNum + "페이지의 내용입니다."); System.out.println(dtoArr[pageNum - 1].toString()); System.out.print("페이지를 삭제하고 싶으시면 'Y'아니면 'N'를 입력하시오\n>>>"); check = sc.next(); if (check.equals("y") || check.equals("Y")) { swit[pageNum - 1] = false; dtoArr[pageNum - 1] = null; export[pageNum - 1] = null; System.out.println("\n페이지가 삭제되었습니다."); } else { System.out.println("\n삭제를 취소합니다."); return; } } // Search - 검색 public void search() { System.out.println(); // 개행 System.out.print("검색하고 싶은 책의 쪽수를 입력하시오(1 ~ 20)\n>>>"); pageNum = sc.nextInt(); if (dtoArr[pageNum - 1] == null) { System.out.println("\n작성된 페이지가 없는 쪽수입니다\n"); return; } System.out.println("\n검색하고 싶은 " + pageNum + "페이지의 내용입니다."); System.out.println(dtoArr[pageNum - 1].toString()); } // Update - 수정 public void update() { System.out.println(); // 개행 System.out.print("수정하고 싶은 책의 쪽수를 입력하시오(1 ~ 20)\n>>>"); pageNum = sc.nextInt(); if (dtoArr[pageNum - 1] == null) { System.out.println("\n작성된 페이지가 없는 쪽수입니다"); return; } System.out.println("\n수정하고 싶은 " + pageNum + "페이지의 내용입니다."); System.out.println(dtoArr[pageNum - 1].toString()); System.out.print("페이지를 수정하고 싶으시면 'Y'아니면 'N'를 입력하시오\n>>>"); check = sc.next(); if (check.equals("y") || check.equals("Y")) { System.out.print("# 나이\n>>>"); int age = sc.nextInt(); System.out.print("# 이름\n>>>"); String name = sc.next(); System.out.print("# 생년월일\n>>>"); String birth = sc.next(); System.out.print("# 주소\n>>>"); String address = sc.next(); dtoArr[pageNum - 1] = new dtoClass(age, name, birth, address, pageNum); System.out.println("\n페이지가 수정되었습니다."); } } // ExportData(in findedFile,writeFile) public void exportData() { Scanner sc = new Scanner(System.in); String fileName; while (true) { System.out.print("\n생성할 파일명\n>>>"); fileName = sc.next(); boolean b = findedFile(fileName); if (b == true) { System.out.println("\n같은 파일이 존재합니다. 파일생성에 실패하였습니다\n"); continue; } File newFile = new File("d:\\tmp\\" + fileName + ".txt"); try { if (newFile.createNewFile() == true) { writeFile(newFile); System.out.println("\n파일을 생성하였습니다."); break; } } catch (IOException e) { System.out.println("\n예외가 발생하였음.. 파일 생성실패"); } } } // findedFile public boolean findedFile(String fname) { File dirFile = new File("d:\\tmp"); String fileList[] = dirFile.list(); boolean b = false; for (int i = 0; i < fileList.length; i++) { if (fileList[i].equals(fname + ".txt") == true) { b = true; break; } } return b; } // writeFile(in checkBeforeWriteFile) public void writeFile(File newFile) { boolean b; b = checkBeforeWriteFile(newFile); if (b == false) { System.out.println("파일에 기입할 수 없습니다."); return; } for (int i = 0; i < export.length; i++) { try { PrintWriter pw; String temp; temp = export[i]; if (temp == null) { temp = (i + 1) + "번째 페이지에는 데이터가 없습니다"; } pw = new PrintWriter(new BufferedWriter(new FileWriter(newFile, b))); pw.println(temp); pw.close(); } catch (IOException e) { e.printStackTrace(); } } } // checkBeforWriteFile public boolean checkBeforeWriteFile(File f) { if (f.canWrite()) { return true; } return false; } // ImportData(in readFile) public void importData() { String ExImport; System.out.print("\n이전 파일을 불러오겠습니까?(y/n)\n>>>"); ExImport = sc.next(); if (ExImport.equals("y") || ExImport.equals("Y")) { export = readFile(); coverUp(); } } // readFile(in checkBeforeReadFile) static String[] readFile() { Scanner sc = new Scanner(System.in); String datas[] = null; String fileName = ""; System.out.print("불러올 파일명을 입력하시오\n>>>"); fileName = sc.next(); File readFile = new File("d:\\tmp\\" + fileName + ".txt"); boolean b = checkBeforeReadFile(readFile); if (!b) { System.out.println("파일이 존재하지 않거나 읽을 수 없다"); return null; } try { BufferedReader br = new BufferedReader(new FileReader(readFile)); // 배열(데이터)의 갯수를 조사 int count = 0; String str; while ((str = br.readLine()) != null) { count++; } br.close(); // 갯수만큼 데이터를 할당(배열) // System.out.println("데이터의 갯수 = " + count); datas = new String[count]; // 데이터를 읽고 배열에 저장 int w = 0; br = new BufferedReader(new FileReader(readFile)); while ((str = br.readLine()) != null) { datas[w] = str; w++; } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return datas; } // checkBeforeReadFile static boolean checkBeforeReadFile(File f) { if (f.exists()) { if (f.isFile() && f.canRead()) { return true; } } return false; } // coverUP public void coverUp() { // 변수선언 int age; String name, birth, address; for (int i = 0; i < export.length; i++) { char c = export[i].charAt(export[i].length() - 1); if ((int) c > 47 && (int) c < 58) { swit[i] = true; String splits[] = export[i].split("-"); age = Integer.parseInt(splits[0]); name = splits[1]; birth = splits[2]; address = splits[3]; pageNum = Integer.parseInt(splits[4]); dtoArr[i] = new dtoClass(age, name, birth, address, pageNum); } else { dtoArr[i] = null; } } } } | cs |
dtoClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package dto; // Data Transfer Object (책의 한 면) public class dtoClass { private int age; private String name; private String birth; private String address; private int pageNum; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirth() { return birth; } public void setBirth(String birth) { this.birth = birth; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public dtoClass() { } public dtoClass(int age, String name, String birth, String address, int pageNum) { super(); this.age = age; this.name = name; this.birth = birth; this.address = address; this.pageNum = pageNum; } @Override public String toString() { return "dtoClass [age=" + age + ", name=" + name + ", birth=" + birth + ", address=" + address + ", pageNum=" + pageNum + "]"; } } | cs |
..
..
==== 사용된 소스 ====
1. 파일 (CRUD) 메소드
2. 문자열.splits("token")
3. 아스키코드를 사용하여 입력값
'오랜된 포스팅 > Java' 카테고리의 다른 글
문자열(String)과 함께 사용되는 함수(method) (0) | 2018.05.28 |
---|---|
ASCII 코드와 replace함수 (0) | 2018.05.28 |
TV 리모콘기능 프로그램 in Java (0) | 2018.05.26 |
데이터의 값의 교환..? Basic Swap (0) | 2018.05.26 |
C.R.U.D. 프로그램 만들기 (0) | 2018.05.26 |