0

如何在 Java 中使用 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;
}
4

2 回答 2

4
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();
}
于 2013-10-27T21:26:44.553 回答
1

根据对您问题的评论,您基本上说您只想验证字符串是否为空。

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

或者,更简单:

String result = executeAdbCommand(adbCmd);
success = result.length() == 0;
于 2013-10-27T21:23:36.787 回答