2013-10-27 41 views
0

如何在Java中使用StringReader讀取字符串的末尾,我不知道字符串的長度是多少。使用StringReader讀取字符串的結尾

這是我多遠,到目前爲止得到:

public static boolean portForward(Device dev, int localPort, int remotePort) 
{ 
    boolean success = false; 
    AdbCommand adbCmd = Adb.formAdbCommand(dev, "forward", "tcp:" + localPort, "tcp:" + remotePort); 
    StringReader reader = new StringReader(executeAdbCommand(adbCmd)); 
    try 
    { 
     if (/*This is what's missing :/ */) 
     { 
      success = true; 
     } 
    } catch (Exception ex) { 
     JOptionPane.showMessageDialog(null, "There was an error while retrieving the list of devices.\n" + ex + "\nPlease report this error to the developer/s.", "Error Retrieving Devices", JOptionPane.ERROR_MESSAGE); 
    } finally { 
     reader.close(); 
    } 

    return success; 
} 
+2

那麼你想用字符串做什麼?目前還不清楚你想要檢查的條件。 –

+0

那麼,我只是想檢查它是否是空的。相當誠實。 – SimonC

+0

StringReader或多或少與任何Reader相同,所以假裝這是一個文件,你會怎麼做你想要的? –

回答

1

根據你的問題,你基本上是說,你只是想驗證字符串是空的註釋。

if (reader.read() == -1) 
{ 
    // There is nothing in the stream, way to go!! 
    success = true; 
} 

,或者更簡單:

String result = executeAdbCommand(adbCmd); 
success = result.length() == 0; 
3
String all = executeAdbCommand(adbCmd); 
if (all.isEmpty()) { 
} 

通常一個StringReader用於讀取/過程分段,並沒有真正適合這裏。

BufferedReader reader = new BufferedReader(
    new StringReader(executeAdbCommand(adbCmd))); 
try 
{ce 
    for (;;) 
    { 
     String line = reader.readLine(); 
     if (line == null) 
      break; 
    } 
} catch (Exception ex) { 
    JOptionPane.showMessageDialog(null, "...", 
     "Error Retrieving Devices", JOptionPane.ERROR_MESSAGE); 
} finally { 
    reader.close(); 
}