IT/JAVA

파일 입출력

_KH_ 2025. 2. 4. 23:42
package class04;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test01 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String path = "C:\\KKH\\resource\\";
		String fileName = "text.txt";
		String saveMsg = null;

		FileReader fr;
		String msg=null;
		try {
			fr= new FileReader(path+fileName); // 파일 읽어들이기
			BufferedReader br = new BufferedReader(fr); 

			while(true) {
				msg = br.readLine();
				if(msg == null) { 
					break; 
				}
			// 27, 31 순서..
				
				System.out.println(msg);
				saveMsg = msg; 
			
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		//System.out.println(saveMsg);
		System.out.println("업다운 게임을 시작합니다!");

		int num;
		int cnt = 0;
		System.out.print("1부터 100까지의 정수 중 입력해주세요 >>");
		while(true) {
			
			
			num = sc.nextInt();
			int save = Integer.parseInt(saveMsg);

			if(num > save) { 
				System.out.println("다운!");
				cnt++;
				System.out.println("1 ~ " + num + " 사이 정수를 다시 입력해주세요!");
				continue;
			} else if(num < save) {
				System.out.println("업!");
				cnt++;
				System.out.println(num + "~ 100 사이의 정수를 다시 입력해주세요!");	
				continue;
			} else // 같은 경우
				System.out.println( (cnt+1) + "번 만에 정답을 맞추셨습니다.");


			if(num == save) {
				break;
			}

		}
		String pathCreate = "C:\\KKH\\resource\\"; 
		String fileNameCreate = "result.txt"; 
		String msgCreate = ("총 " + (cnt+1) + "번만에 정답을 맞추셨습니다."); 

		FileWriter fw = null; 
		try {
			fw = new FileWriter(pathCreate + fileNameCreate); 

			fw.write(msgCreate);

		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			  if (fw != null) {
			        try {
			            fw.close();
			        } catch (IOException e) {
			            e.printStackTrace();
			        }
			    }
			    System.out.println("로그 : 파일 쓰기(출력)가 완료되었습니다.");
		}
	}
}
package class04;

import java.util.Scanner;

class Account {
	static int balance = 10000;

	synchronized void withdrawal() {

		int money;
		while(true) {
			Scanner sc = new Scanner(System.in);
			System.out.print("출금하실 금액을 입력해주세요(숫자만 입력) >>");
			money = sc.nextInt();
			if(money >=0 && money <= 10000) {
				break;
			}
		}
		
		if(Account.balance < money) {
			System.out.println("잔액 부족으로 인해 인출 불가!");
			return;
		} else if(Account.balance == money) {
			Account.balance -= money;
			return;
		}
		Account.balance -= money;
		System.out.println(balance);
		
		//System.out.println(Person(Thread) + "가 " + money +"원을 출금했습니다.");
				// 누가 얼마를 출금했습니다. 라고 출력
		//System.out.println("남은 금액은 " + balance + "원입니다.");
		

		
	}
}

class Person implements Runnable {
	Account a;


	Person(Account a) {
		this.a = a;
	}

	@Override
	public void run() { 
		a.withdrawal();
	}
}



public class Test02 {

	public static void main(String[] args) {

		Account a = new Account();


		Thread mom = new Thread(new Person(a));
		Thread dad = new Thread(new Person(a));
		Thread me = new Thread(new Person(a));
		Thread brother = new Thread(new Person(a));
		
		mom.start();
		dad.start();
		me.start();
		brother.start();


	}

}