traceroute 명령을 수행하기위한 Android app을 작성 중입니다.Java에서 하나의 긴 String에서 IP 주소를 추출하는 방법
지금은 traceroute가 실행될 때 콘솔 출력을 매우 긴 단일 문자열로 지정합니다. 이것은 다음과 같은 코드에 의해 수행됩니다
public void runAsRoot(String[] cmds) throws Exception {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
InputStream is = p.getInputStream();
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
int readed = 0;
byte[] buff = new byte[4096];
// if cmd requires an output
// due to the blocking behaviour of read(...)
boolean cmdRequiresAnOutput = true;
if (cmdRequiresAnOutput) {
while(is.available() <= 0) {
try { Thread.sleep(10000); } catch(Exception ex) {} //timeout.. gotta watch this carefully.
}
while(is.available() > 0) {
readed = is.read(buff);
if (readed <= 0) break;
String seg = new String(buff,0,readed);
System.out.println("#> "+seg);
ListofIPs = seg;
}
}
}
os.writeBytes("exit\n");
os.flush();
}
이 문자열의 출력은 다음과 같습니다
내가 뭘 원하는이 출력을 만 IP 주소를 추출 순서에 넣어이다 배열로. 이것은 내가 어디서부터 시작될지라도 내가 잃어버린 곳입니다. 나는 어떤 종류의 문자열 조작을 생각하고 있지만 어디에서 시작해야할지 모른다.
누구나 아이디어 나 조언이 있다면 크게 감사하겠습니다.
미리 감사드립니다.
'String' 클래스의'.split()'메소드를보십시오. – fge
정규식에 익숙합니까? 그렇지 않다면 여기에서 확인하십시오. http://en.wikipedia.org/wiki/Regex – hellerve
줄 단위로 읽기 ... .split ("")'두 번째 요소는 IP 주소입니다. – Shivam