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 に答える