반응형

문제 1, 회원정보를 파일로 관리.

1. 초기

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		Article article = new Article();
		
		System.out.println("== 회원 프로그램 시작 ==");
		System.out.println("1) 저장하기");
		System.out.println("2) 불러오기");
		System.out.println("3) 출력하기");
		
		
		while(true) {
			System.out.printf("명령) ");
			String command = scanner.next();
			
			if (command.equals("1")) {
				scanner.nextLine();
				try {
					OutputStream output = new FileOutputStream("D:/a.txt", true); // txt문서내에 입력값 연달아 저장가능
					System.out.println("== 저장하기 시작 ==");
					
					System.out.printf("로그인 아이디 : ");
					String loginId = scanner.next();
					scanner.nextLine();
					System.out.printf("로그인 비번 : ");
					String loginPw = scanner.next();
					scanner.nextLine();
					String str = "아이디 : " + loginId + " 비밀번호 : " + loginPw;
					byte[] by = str.getBytes();
					output.write(by);
					
					System.out.printf("%d.txt 파일이 생성되었습니다.\n", 1);
					System.out.println("== 저장하기 끝 ==");
				} catch (Exception e) {
					e.getStackTrace();
				}
			}
			
		}		
	}	
}
class Article {
	int id;
	String loginId;
	String loginPw;
}

 

2. 개선

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		Article article = new Article();

		showHelp();

		int num = 1;

		while (true) {
			System.out.printf("명령) ");
			String command = scanner.next();

			if (command.equals("1")) {
				scanner.nextLine();
				try {
					OutputStream output = new FileOutputStream("D:/" + num + ".txt");
					System.out.println("== 저장하기 시작 ==");

					String id = command;
					String regDate = getNowdate();
					System.out.printf("로그인 아이디 : ");
					String loginId = scanner.next();
					scanner.nextLine();
					System.out.printf("로그인 비번 : ");
					String loginPw = scanner.next();
					scanner.nextLine();
					String str = "{\"id\": " + id + ", \"regDate\": \"" + regDate + "\", \"loginId\":\"" + loginId
							+ "\", \"loginPw\":\"" + loginPw + "\"}";
					byte[] by = str.getBytes();
					output.write(by);

					System.out.printf("%d.txt 파일이 생성되었습니다.\n", num);
					System.out.println("== 저장하기 끝 ==");
				} catch (Exception e) {
					e.getStackTrace();
				}
				num++;
			} else if (command.equals("2")) {
				System.out.println("== 불러오기 시작 ==");
				System.out.printf("번호) ");
				String temp = scanner.next();
				scanner.nextLine();

				try {
					/*
					// 이 경우엔 한 글자씩 불러온다.
					// 파일 객체 생성
					String file = new File("D:/" + temp + ".txt");
					// 입력 스트림 생성
					FileReader file_reader = new FileReader(file);
					int cur = 0;
					while ((cur = file_reader.read()) != -1) {
						System.out.print((char) cur);
					}
					file_reader.close();
					*/
					
					// 이 경우엔 한 줄로 불러온다.
					// 파일 객체 생성
					String path = "D:/" + temp + ".txt";
					
					BufferedReader br = new BufferedReader(new FileReader(path));
					String abc = br.readLine();
					
					System.out.println(abc);			
					
					br.close();					
				} catch (FileNotFoundException e) {
					e.getStackTrace();
				} catch (IOException e) {
					e.getStackTrace();
				}

				System.out.println("- 불러오기 완료");
				System.out.println("== 불러오기 끝 ==");
			} else if (command.equals("3")) {
				System.out.println("== 출력하기 시작 ==");
				System.out.printf("번호 : %s\n", article.id);
				System.out.printf("날짜 : %s\n", article.regDate);
				System.out.printf("로그인 아이디 : %s\n", article.loginId);
				System.out.printf("로그인 비번 : %s\n", article.loginPw);
				System.out.println("== 출력하기 끝 ==");
			}

		}
	}

	static void showHelp() {
		System.out.println("== 회원 프로그램 시작 ==");
		System.out.println("1) 저장하기");
		System.out.println("2) 불러오기");
		System.out.println("3) 출력하기");
	}

	static String getNowdate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat Date = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		String regDate = Date.format(cal.getTime());
		return regDate;
	}
}

class Article {
	String id;
	String regDate;
	String loginId;
	String loginPw;
}
반응형

+ Recent posts