使用StringReader读取String的结尾

时间:2013-10-27 21:15:52

标签: java java-io stringreader

如何使用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;
}

2 个答案:

答案 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();
}

答案 1 :(得分:1)

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

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

或者,甚至更简单:

String result = executeAdbCommand(adbCmd);
success = result.length() == 0;