2
입력 스트림을 지우기/재설정/열기하여 Java의 2 가지 다른 메소드에서 사용할 수 있습니까?
다음은 코드입니다 : package testpack;
import java.io.*;
public class InputStreamReadDemo {
private void readByte() throws IOException {
System.out.print("Enter the byte of data that you want to be read: ");
int a = System.in.read();
System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
// tried writing System.in.close(); and System.in.reset();
}
private void readLine() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line of text: ");
String res = br.readLine();
System.out.println("You entered the following line of text: "+res);
// ... and tried writing br.close();
// but that messes things up since the input stream gets closed and reset..
}
public static void main(String[] args) throws IOException {
InputStreamReadDemo isrd = new InputStreamReadDemo();
isrd.readByte(); // method 1
isrd.readLine(); // method 2
}
}
다음과 같이 출력합니다. 첫 번째 메서드가 호출 될 때 "Something"을 입력하면 두 번째 메서드는 자동으로 "omething"을 읽습니다. 다시 입력 ..) :
run:
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: You entered the following line of text: omething
BUILD SUCCESSFUL (total time: 9 seconds)
시도해 보았지만 작동하지 않았습니다. 프로그램은 이전과 동일하게 작동합니다. –
죄송합니다. 방금 게시 한 코드를 복사하고 readByte 끝에 제안한 행을 넣은 다음 응용 프로그램을 컴파일하고 실행했습니다. 그것은 나를 위해 작동합니다. – Mjonir74
그건 정말 이상한 ..이게 내가 실행하고있는 것입니다 : http://pastebin.com/5P5pKYxs 그것은 위의 코드와 동일합니다. 그렇지 않습니다. –