2011-10-06 1 views
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) 

답변

2

가 출력합니다 이유 "omething는"내의 readByte에 전화, 당신은 당신의 입력의 첫 번째 바이트를 소비한다는 것입니다. 그런 다음 readLine은 스트림의 나머지 바이트를 사용합니다.

은 내의 readByte 방법의 끝에이 줄을보십시오 : 아직 내의 readLine에 대한 다음 입력을 소비 열려 스트림을 유지하면서 System.in.skip(System.in.available());

스트림에 ("omething")를, 나머지 바이트를 건너 것이다.

일반적으로 System.in 또는 System.out을 닫지 않는 것이 좋습니다. 에서

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+"."); 
     System.in.skip(System.in.available()); 
    } 
    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 
    } 
} 

결과 :

$ java InputStreamReadDemo 
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: Another thing 
You entered the following line of text: Another thing 

UPDATE : 우리가 채팅에서 설명하고있는 바와 같이, 그것은 명령 줄에서 작동하지만,하지 넷빈즈한다. IDE가 있어야합니다.

+0

시도해 보았지만 작동하지 않았습니다. 프로그램은 이전과 동일하게 작동합니다. –

+0

죄송합니다. 방금 게시 한 코드를 복사하고 readByte 끝에 제안한 행을 넣은 다음 응용 프로그램을 컴파일하고 실행했습니다. 그것은 나를 위해 작동합니다. – Mjonir74

+0

그건 정말 이상한 ..이게 내가 실행하고있는 것입니다 : http://pastebin.com/5P5pKYxs 그것은 위의 코드와 동일합니다. 그렇지 않습니다. –

관련 문제