명품 JAVA 프로그래밍

명품 JAVA 프로그래밍 8장 실습문제 1

anycoding 2021. 12. 8. 16:34
반응형

문제

Scanner로 입력받은 이름과 전화번호를 한 줄에 한 사람씩 c:\temp\phone.txt파일에 저장하라. 
"그만"을 입력하면 프로그램을 종료한다.

 

입력 예시

전화번호 입력 프로그램입니다.
이름 전화번호 >> 황기태 010-5555-7777
이름 전화번호 >> 이재문 011-3333-4444
이름 전화번호 >> 김남윤 065-2222-1111
이름 전화번호 >> 그만

 

소스 코드

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Main1 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		FileWriter fout = null;
		System.out.println("전화번호 입력 프로그램입니다.");
		try {
			fout = new FileWriter("파일 위치");
			while(true) {
				System.out.print("이름 전화번호 >> ");
				String line = scanner.nextLine();
				if(line.length()==0 || line.equals("그만"))
					break;
				fout.write(line,0,line.length());
				fout.write("\r\n",0,2);
			}
			fout.close();
		}catch(IOException e) {
			System.out.println("입출력 오류");
		}
		scanner.close();
	}

}

phone.txt 가 아닌 test.txt로 저장하였습니다.

test.txt
0.00MB

반응형