2013-09-28 1 views
0

나는 비지 박스의 버전을 반환 oupout 터미널에서의 첫 번째 라인을 실행이 코드버퍼링 된 리더에서 처음 5 문자를 읽는 방법은 무엇입니까?

Process p =Runtime.getRuntime().exec("busybox"); 
     InputStream a = p.getInputStream(); 
     InputStreamReader read = new InputStreamReader(a); 
     BufferedReader in = new BufferedReader(read); 

있습니다. 내가하는 것처럼 처음 5자를 예로 들면?

+0

버퍼링 된 리더에서 처음 5자를 읽으시겠습니까? –

+0

예 .............. –

+0

@ MariocciRossini-subString (int startposition, int endposition)을 사용하면 ... 이것이 도움이된다고 생각합니다. – FarhaSameer786

답변

0

이 방법을 수행

String line = in.readLine(); 
if(line!=null && line.length() >5) 
    line = line.substring(0, 5); 
0

시도

Process p; 
     try { 
      p = Runtime.getRuntime().exec("busybox"); 
      InputStream a = p.getInputStream(); 
      InputStreamReader read = new InputStreamReader(a); 
      BufferedReader in = new BufferedReader(read); 
      StringBuilder buffer = new StringBuilder(); 
      String line = null; 
      try { 
       while ((line = in.readLine()) != null) { 
        buffer.append(line); 
       } 

      } finally { 
       read.close(); 
       in.close(); 
      } 

      String result = buffer.toString().substring(0, 15); 
      System.out.println("Result : " + result); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

출력

결과 : 다른 답변이 너무 잘 다음 작업을해야하지만 비지 박스의 v1.13.3

+0

이어야합니다. buffer.toString(). substring (0, 5)? – upog

2

다시 종료하고 스트림을 닫습니다. 다섯 글자 012 :

Process p = Runtime.getRuntime().exec("busybox"); 
    InputStream a = p.getInputStream(); 
    InputStreamReader read = new InputStreamReader(a); 

    StringBuilder firstFiveChars = new StringBuilder(); 

    int ch = read.read(); 

    while (ch != -1 && firstFiveChars.length() < 5) { 
     firstFiveChars.append((char)ch); 
     ch = read.read(); 
    } 

    read.close(); 
    a.close(); 

    System.out.println(firstFiveChars);