본문 바로가기
IT관련/JAVA_자유 노트

[수업창작] 2020-05-18 회원정보 파일저장

by XoX 2020. 5. 19.
728x90
개인적인 메모를 위해 작성된 글이므로 가벼운 참고용으로 봐주세요.

 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.text.*; 
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

class Main{
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Board board = new Board();
		board.setScanner(scanner);
		board.start();
	}
}
class Board{
	Scanner scanner;
	void setScanner(Scanner scanner) {
		this.scanner = scanner;
	}
	String loginPw;
	String loginId;
	String regDate;
	String id;
	int lastId;
	int callId;
	private OutputStream output;
	void start() {
		doCommandHelp();
		while(true) {
			System.out.print("명령EX) 숫자[엔터] ");
			String command = scanner.next().trim();
			int commandNum = Integer.parseInt(command);
			if(commandNum==1) {
				scanner.nextLine();
				doCommandSave();
			} else if(commandNum == 2) {
				scanner.nextLine();
				doCommandCall();
			} else if(commandNum == 3) {
				scanner.nextLine();
				doCommandPrint();
			} else if(command.length() >= 0) {
				System.out.printf("'%s'는 존재하지 않는 명령어입니다. 다시 입력해주세요.\n",command);
			}
		}
	}
	
	void doCommandHelp() {
		System.out.println("1) 저장하기");
		System.out.println("2) 불러오기");
		System.out.println("3) 출력하기");
	}
	void doCommandSave() {
		try {
		    output = new FileOutputStream("C:/Users/ginit/Desktop/coding/member.txt");
		    System.out.println("==저장하기 시작==");
			System.out.print("로그인 아이디: ");
			loginId = scanner.nextLine();
			System.out.print("로그인 비번: ");
			loginPw = scanner.nextLine();
			regDate = nowDate();
			lastId++;
			id = Integer.toString(lastId);
			System.out.printf("%d.txt파일이 생성되었습니다.\n",lastId);
			System.out.println("==저장하기 끝==");
			String member = "{\n" + 
                    "\t\"id\": " + id + ",\n" + 
                    "\t\"regDate\" : " + "\"" + regDate + "\",\n" + 
                    "\t\"loginId\" : " + "\"" + loginId + "\",\n" + 
                    "\t\"loginPasswd\" : " + "\"" + loginPw + "\"\n" + 
                    "}\n";
			byte[] arr = member.getBytes();
		    output.write(arr);
		} catch (Exception e) {
	            e.getStackTrace();
		}		
	}
	void doCommandCall() {
		System.out.print("번호: ");
		callId = scanner.nextInt();
	}
	void doCommandPrint() {
		System.out.println("==불러오기 시작==");
		try{
			File file = new File("C:/Users/ginit/Desktop/coding/member.txt");
			FileReader file_reader = new FileReader(file);
			int cur = 0;
			while((cur = file_reader.read()) != -1){
			System.out.print((char)cur);
			}
		file_reader.close();
		}
		catch (FileNotFoundException e) {
			e.getStackTrace();
		}
		catch(IOException e){
			e.getStackTrace();
		}
		System.out.println("==불러오기 끝==");
	}

	String nowDate() {
		SimpleDateFormat formatter = new SimpleDateFormat ( "yyyy.MM.dd HH:mm:ss", Locale.KOREA );
		Date currentTime = new Date ( );
		String dTime = formatter.format ( currentTime );
		return dTime;
	}
}
class Member{
	String loginId;
	String loginPw;
	String regDate;
	String id;
}
728x90
반응형

댓글