-
์ฝ์ ์ ์ถ๋ ฅ ScannerJava 2022. 4. 11. 11:59
๐ ์ฝ์
: ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ฐ๊ฑฐ๋ ์ฌ์ฉ์์๊ฒ ๋ฌธ์์ด์ ์ถ๋ ฅํด ์ฃผ๋ ์ญํ ์ ํ๋ ๊ฒ์ ํต์นญํ๋ ๋ง
* ์ฝ์์ฐฝ์์ ํด๋น ๋ฌธ์์ด์ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ "์ฝ์ ์ถ๋ ฅ"
ํด๋น ์ง๋ฌธ์ ์ฌ์ฉ์๊ฐ ๋ต๋ณ์ ์ ๋ ฅํ๋ ๊ฒ์ "์ฝ์ ์ ๋ ฅ"
* ์ํฐํค๋ฅผ ์ ๋ ฅํด์ผ ์ฌ์ฉ์์ ์ ๋ ฅ์ด ์ข ๋ฃ๋๊ณ ํ๋ก๊ทธ๋จ์ ์ ๋ฌ๋๋ค
* ์ ๋ ฅํ ๋ฌธ์์ด์ ์ป๊ธฐ ์ํด์๋ ์๋ฐ์ System.in์ ์ฌ์ฉํ๋ค
๐ก InputStream
: InputStream์ read๋ฉ์๋๋ 1byte๋ง ์ฝ๋๋ค -> ์์คํค์ฝ๋๋ก ์ฝ์ด๋ค์ธ๋ค
* ์คํธ๋ฆผ : ์ด์ด์ ธ ์๋ ๋ฐ์ดํฐ(byte)์ ํํ
import java.io.IOException; import java.io.InputStream; public class Sample { public static void main(String[] args) throws IOException { InputStream in = System.in; int x; x = in.read(); System.out.println(x); // ํ๊ฐ byte[] a = new byte[3]; in.read(a); System.out.println(a[0]); System.out.println(a[1]); System.out.println(a[2]); } }
๐ก InputStreamReader
: ์ ๋ ฅํ ๋ฌธ์ ๊ทธ๋๋ก ์ถ๋ ฅ๋๋ค
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Sample { public static void main(String[] args) throws IOException { InputStream in = System.in; InputStreamReader reader = new InputStreamReader(in); char[] a = new char[3]; reader.read(a); System.out.println(a); } }
๐ก BufferedReader
: ๊ณ ์ ๊ธธ์ดX => ์ฌ์ฉ์๊ฐ ์ํฐํค๋ฅผ ์ ๋ ฅํ ๋๊น์ง ์ฌ์ฉ์์ ์ ๋ ฅ ์ ๋ถ ๋ฐ๊ธฐ
import java.io.IOException; import java.io.BufferedReader; public class Sample { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(reader); String a = br.readLine(); System.out.println(a); } }
๐ ๊ธฐ์ต
InputStream - byte
InputStreamReader - character
BufferedReader - String
๐ก Scanner
โ next() - ๋จ์ด ํ๋(Token)
โ nextLine() - ๋ผ์ธ
โ nextInt - ์ ์
import java.util.Scanner; public class Sample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(sc.next()); } }
* System.in == ์ฝ์์ ๋ ฅ
๐ก ์ฝ์ ์ถ๋ ฅ
public class Sample { public static void main(String[] args) { System.out.println("์ผ๋ฐ ์ถ๋ ฅ"); System.err.println("์๋ฌ ์ถ๋ ฅ"); } }
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๋ค Lambda (0) 2022.04.11 ๊ธฐ๋ณธ ํด๋์ค(Object, String, Class) (0) 2022.03.06 ์ธํฐํ์ด์คinterface (0) 2022.03.05 ๋งต(Map), ์งํฉ(Set), ์์์งํฉ(Enum) (0) 2022.03.05 ํ ํ๋ฆฟ ๋ฉ์๋ (0) 2022.03.04