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