【问题标题】:In Android system, hasNextLine() return false after reading the first line在Android系统中,hasNextLine()读取第一行后返回false
【发布时间】:2012-04-16 05:54:39
【问题描述】:

在Android系统中,我想读取文件“/proc/net/xt_qtaguid/stats”。但是,如果未能阅读第二行。 读取第一行就成功了。但是,hasNextLine() 返回 false,这使得无法读取下一行。

代码如下:

    File statsFile = new File("/proc/net/xt_qtaguid/stats");
    Scanner scanner = null;
    try {
        scanner = new Scanner(statsFile);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            Log.d(TAG, "hasNextLine(): " + scanner.hasNextLine());

这是我试图阅读的统计文件。该文件在这篇文章中看起来像一行。但是,每行以“0x0A”结尾,分3行显示。

idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packets rx_tcp_bytes rx_tcp_packets rx_udp_bytes rx_udp_packets rx_other_bytes rx_other_packets tx_tcp_bytes tx_tcp_packets tx_udp_bytes tx_udp_packets tx_other_bytes tx_other_packets 2 wlan0 0x0 0 0 1129 10 2066 32 80 2 1049 8 0 0 0 0 530 8 1536 24 3 wlan0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

【问题讨论】:

  • @如果你从文件中读取,为什么不使用一些'Readers'

标签: android java.util.scanner


【解决方案1】:

尝试使用好的旧阅读器类:

try {
    File statsFile = new File("/proc/net/xt_qtaguid/stats");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(statsFile)));

    String line;
    while((line = br.readLine()) != null){
        Log.d(TAG, "readline(): " + line);
    }

    br.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

【讨论】:

    猜你喜欢
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多