콘솔(Console) : 시스템을 사용하기 위해 키보드로 입력을 받고 화면으로 출력하는 소프트웨어
Unix,Linux : 터미널
Windows 운영체제 : 명령 프롬프트
Console뷰 : 이클립스
InputStream is = System.in
int asciiCode = is.read(); //읽은 byte는 키보드의 아스키 코드
char inputChar = (char) is.read(); //아스키 코드로부터 문자 변환
char inputChar = (char) 97;
InputStream의 read() 메소드는 1바이트만 읽기 때문에 1바이트의 아스키 코드로 표현되는 숫자, 영문자, 특수문자는 잘 읽음 ❌ 한글과 같이 2바이트를 필요로 하는 유니코드는 읽을 수 없음
키보드로 입력된 한글을 얻기 위해서는 read(byte[] b)메소드나 read(byte[] b, int off, int len)메소드로 전체 입력된 내용을 바이트 배열로 받고, 이 배열을 이용해서 String 객체를 생성함
요약 : read()메소드는 1바이트씩만 읽음 <- 오류 발생
전체 내용을 바이트 배열로 받아 String 객체 생성 후 읽기
최대 영문자 15자 , 한글 7자를 저장할 수 있는 바이트 배열의 예
byte[] byteData = new byte[15];
int readByteNo = System.in.read(byteData);
//바이트배열, 시작 인덱스, 읽은 바이트 수-2
Strig strData = new String(byteData, 0, readByteNo -2)
프로그램에서 바이트 배열에 저장된 아스키 코드를 사용하려면 문자열로 변화해야 함, 변환할 문자열은 바이트 배열의 0번 인덱스에서 시작해서 읽은 바이트 수에서 2를 뺌, Enter키에 해당하는 마지막 두 바이트 제외
InputStream is = System.in;
byte[] datas = new byte[100];
System.out.print("이름:");
int nameBytes = is.read(datas);
String name =. new String(datas, 0, nameBytes-2);
byte b = 97;
os.write(b) //'a' 가 출력됨
os.flush();
ex. 콘솔로 한글 출력
write()메소드는 1바이트만 보내서 숫자,영문자,특수문자는 잘 읽음 ❌ 한글 2byte를 필요로 하는 유니코드는 출력할 수 없음
⭕️ 한글로 출력 시 한글을 바이트 배열로 얻은 후 write(byte[] b), write(byte[] b,int off, int len)로 출력
String name = "홍길동"
byte[] nameBytes = name.getBytes();
os.write(nameBytes);
os.flush();
OutputStream os = System.out;
for(byte b = 48; b<58; b++) {os.write(b)}
os.write(13);
for(byte b = 97; b<123; b++) {os.write(b);}
os.write(13);
String hangul = "가나다라마바사아자차카타파하";
byte[] hangulBytes = hangul.getBytes();
os.write(hangulBytes);
os.flush();
//결과 : 0123456789
// abcdefghijklmnopqrstuvwxyz
// 가나다라마바사아자차카타파하
PrintStream ps = System.out;
ps.println(...); //== System.out.println(...);
자바6부터 콘솔에서 입력된 문자열을 쉽게 읽을 수 있도록 제공
java.io.Console 이클립스에서 System.console()은 null리턴
Console console = System.console(); //이클립스는 여기서 null이 난다.
System.out.println("아이디 : ");
String id = console.readLine();
System.out.println("패스워드 : ");
char[] charPass = console.readPassword();
//String strPassword = new String(charPass);
System.out.println(id);
System.out.println(charPass);
😰 단점 : 문자열은 읽을 수 있지만 기본 타입(정수,실수)값을 바로 읽을 수 없음
java.io 패키지 아님
콘솔로부터 기본 타입의 값을 바로 읽을 수 있음
Scanner scanner= new Scanner(System.in)
Scanner scanner = new Scanner(System.in);
System.out.println("문자열 입력> ");
String inputString = scanner.nextLine();
System.out.println(inputString);
System.out.println("정수입력> ");
int inputInt = scanner.nextInt();
System.out.println(inputInt);
System.out.println("실수입력>");
double inputDouble = scanner.nextDouble();
System.out.println(inputDouble);