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

[수업창작] 20-05-20(파일로 저장)

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

 

날짜와 관련된 메서드를 빼면 제대로 실행된다.

그런데 그게 있으면 오류가 난다는거..

package pair_program;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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);
		Member member = new Member();
		member.setScanner(scanner);
		member.start();
	}
}

class Member{
	Scanner scanner;
	String newString;
	private OutputStream output;
	void setScanner(Scanner scanner) {
		this.scanner = scanner;
	}
	int num ;
	int id;
	String regDate;
	String loginId;
	String loginPw;
	void start() {
		commandList();
		while(true) {
			System.out.print("명령) ");
			int command = scanner.nextInt();
			if(1==command) {
				scanner.nextLine();
				doCommandSave();
			} else if(2==command) {
				scanner.nextLine();
				doCommandCall();
			} else if(3==command) {
				scanner.nextLine();
				doCommandPrint();
			}
		}
	}
	void commandList() {
		System.out.println("1) 저장하기");
		System.out.println("2) 불러오기");
		System.out.println("3) 출력하기");
	}
	void doCommandSave() {
		System.out.println("== 저장하기 시작 ==");
		try {
		   
		    num ++;
		    int id = num;
		    output = new FileOutputStream(num + ".txt");
		    System.out.print("아이디: ");
		    String loginId = scanner.nextLine();
		    System.out.print("비밀번호: ");
		    String loginPw = scanner.nextLine();
		    String regDate = nowDate();
		    String member = String.format("{\"id\":%d,\"regDate\":%s,\"loginId\":%s,\"loginPw\":%s}", id, regDate, loginId, loginPw);
		    byte[] by=member.getBytes();
		    output.write(by);
				
		} catch (Exception e) {
	            e.getStackTrace();
		}
		System.out.println("== 저장하기 끝 ==");
	}
	String nowDate() {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH~mm~ss");
		Calendar time = Calendar.getInstance();

		return format1.format(time.getTime());
	}
	void doCommandCall() {
		try {
			System.out.printf("파일번호 : ");
			int num = scanner.nextInt();
			scanner.nextLine();
		    String filePath = "C:/work/sts-4.4.0.RELEASE-workspace/pair_program/" + num + ".txt";
		    FileInputStream fileStream = null; // 파일 스트림 
	        fileStream = new FileInputStream( filePath );// 파일 스트림 생성
	        byte[ ] readBuffer = new byte[fileStream.available()];
	        while (fileStream.read( readBuffer ) != -1){}
	        newString = new String(readBuffer) ;
	        fileStream.close();
		    } catch (Exception e) {
			e.getStackTrace();
		    }
	}
	void doCommandPrint() {
		String rs = newString;
		rs = rs.replace("{", "");
		rs = rs.replace("}", "");
		String[] bits = rs.split(",");

		for (int i = 0; i < bits.length; i++) {
			String[] fieldBits = bits[i].split(":");
			
			
			String fieldName = fieldBits[0];			
			fieldName = fieldName.replace("\"", "");
			String fieldValue = fieldBits[1];
			if(i == 2) {
				System.out.println(fieldBits[0]);
				System.out.println(fieldBits[1]);
			}
			
			if (fieldName.equals("id")) {
				this.id = Integer.parseInt(fieldValue);
			} else if (fieldName.equals("regDate")) {
				this.regDate = fieldValue;
			} else if (fieldName.equals("loginId")) {
				this.loginId = fieldValue;
			} else if (fieldName.equals("loginPw")) {
				this.loginPw = fieldValue;
			}
		}
		
		System.out.printf("번호: %d\n", id);
		System.out.printf("날짜: %s\n", regDate);
		System.out.printf("아이디: %d\n", loginId);
		System.out.printf("비밀번호: %d\n", loginPw);
	}
}
728x90
반응형

댓글