File I/O


File f = new File(SAVE_FILE);
try {
    FileWriter fw = new FileWriter(f, true);
    fw.write(date + "," + plan + "\n");
    fw.close();
} catch (IOException e) {
    e.printStackTrace();
}
try {
    BufferedReader br = new BufferedReader(new FileReader(SAVE_FILE));
    while (true) {
        String line = null;
        try {
            line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (line == null) break;
        String date = line.split(",")[0];
        String detail = line.split(",")[1];
        planner.put(date, detail);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Scanner ์™€ BufferedReader


Scanner

import java.util.Scanner;
 
public class study {
 
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
 
		System.out.println("์›ํ•˜๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
		String input = scanner.nextLine();
		int num = Integer.parseInt(input);
 
		System.out.println(num);
	}
}
  • ๊ณต๋ž€๊ณผ ์ค„๋ฐ”๊ฟˆ ๋ชจ๋‘ ์ž…๋ ฅ๊ฐ’์˜ ๊ฒฝ๊ณ„๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‰ฌ์šด ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅ
  • ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Œ (๋ฌธ์ž์—ด ํŒŒ์‹ฑ ๊ธฐ๋Šฅ ์ œ๊ณต)

BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class study {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // ์„ ์–ธ
 
		String s = br.readLine(); 
		int i = Integer.parseInt(br.readLine()); 
		
		System.out.println("String : " + s);
		System.out.println("Int : " + i);
		
	}
}
  • InputStreamReader ์— ๋ฒ„ํผ๋ง ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ ํด๋ž˜์Šค
    • InputStreamReader ๋Š” ๋ฌธ์ž์—ด์„ Character ๋‹จ์œ„๋กœ ์ฝ์Œ
  • ์ผ์ •ํ•œ ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์ฝ์–ด์™€ ๋ฒ„ํผ์— ๋ณด๊ด€ ํ›„ ๋ฒ„ํผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹
  • ๋ผ์ธ ๋‹จ์œ„๋กœ ์ž…๋ ฅ ๋ฐ›๊ณ  String ํƒ€์ž…์œผ๋กœ๋งŒ ์ž…๋ ฅ๋จ
  • ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Œ

Difference