객체 8장 과제

IkSun·2023년 5월 25일

실습 8_1

import java.util.Scanner;
import java.io.*;

public class Ex_8_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("전화번호 입력 프로그램입니다.");
		FileWriter fout = null;
		File f = new File("C:\\temp\\phone.txt");
		try {
			fout = new FileWriter(f);
			while(true) {
				System.out.print("이름 전화번호 >> ");
				String line = scanner.nextLine();
				if(line.equals("그만")) {
					System.out.println(f.getPath() + "에 저장하였습니다.");
					break;
				}
				fout.write(line);
				fout.write("\r\n");
			}
			fout.close();	
		} catch (IOException e) {
			System.out.print("입출력 오류");
		}
		scanner.close();
	}

}
//result
전화번호 입력 프로그램입니다.
이름 전화번호 >> 황기태 010-5555-7777
이름 전화번호 >> 이재문 011-3333-4444
이름 전화번호 >> 김남윤 065-2222-1111
이름 전화번호 >> 그만
C:\temp\phone.txt에 저장하였습니다.

실습 8_2

import java.io.*;

public class Ex_8_2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File f = new File("C:\\temp\\phone.txt");
		FileReader fin = null;
		
		System.out.println(f.getPath() + "를 출력합니다.");
		try {
			fin = new FileReader(f);
			int c;
			while((c=fin.read()) != -1)
				System.out.print((char)c);
		} catch(IOException e) {
			System.out.println("입출력 오류");
		}
	}

}
//result
C:\temp\phone.txt를 출력합니다.
황기태 010-5555-7777
이재문 011-3333-4444
김남윤 065-2222-1111



실습 8_7

import java.io.*;
public class Ex_8_7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File src = new File("C:/temp/moon.jpeg");
		File dest = new File("C:/temp/b.jpeg");
		
		int c;
		try {
			FileInputStream fi = new FileInputStream(src);
			FileOutputStream fo = new FileOutputStream(dest);
			
			System.out.println(src.getPath() + "를 " + dest.getPath() + "로 복사합니다.");
			System.out.println("10%마다 *를 출력합니다.");
			long f_size = ((src.length()) / 10);
			
			byte [] buf = new byte[(int)f_size];
			while(true) {
				int n = fi.read(buf);
				fo.write(buf, 0, n);
				
				if(n<buf.length) break;
				System.out.print('*');
			}
			fi.close();
			fo.close();
			
		} catch (IOException e) {
			System.out.println("입출력 오류");
		}
	}

}
//result
C:\temp\moon.jpeg를 C:\temp\b.jpeg로 복사합니다.
10%마다 *를 출력합니다.
**********



profile
공부한 것 기록용

0개의 댓글