簡體   English   中英

在java中讀取到文件末尾

[英]Read to the end of file in java

我確實通過 Java 在 Linux 中讀取串行接口文件。 有時我只需要丟棄所有數據並只讀取新數據。

說明:有大量數據並且新數據即將到來,因此我必須丟棄現有緩沖區並開始等待新數據。 外部板只是不斷發送。 我最終閱讀了舊數據,只是在幾次迭代中我就有了當前值。 我只需要跳到結尾並等待新的數據集,而不是閱讀所有舊的廢話。

String file = "/dev/ttyO1";
FileInputStream inputStream  = new FileInputStream(file);

private static byte[] readUntil(InputStream in, int timeout) throws IOException {
//        long lastTime = System.currentTimeMillis();
    while (true) {
        if (in.available() > 0) {
            if (in.read() == 83)
                break;
        }
        try { Thread.sleep(20); } catch (Exception e) {}
    }
    byte[] text = new byte[10];
    for (int i = 0; i < 10; i++) {
        text[i] = (byte) in.read();
        if (text[i]=="E".getBytes()[0]) break;
        try { Thread.sleep(20); } catch (Exception e) {}
    }
    in.read(); // just read last one
    return text;
}

我只是不知道如何丟棄現有數據並只讀取新數據。

我假設您真正想要的是刷新串行端口的傳入緩沖區中的所有數據。

在 Linux 上,在 C 程序中, 您可以執行以下操作

tcflush(fd, TCIFLUSH)

刷新傳入緩沖區。 但是您將無法直接從 Java 執行此操作 - 它不是獨立於平台的功能。

您可以編寫一個小的 C 程序來執行此操作,然后將數據從/dev/ttyO1到 stdout。 您可以使用ProcessBuilder從 Java 代碼啟動該程序並讀取其數據,而不是直接讀取串行端口。


再想一想,你不需要C程序來做任何管道,你只需要調用一次,然后你就可以從Java打開/dev/tty01

這是一個可以執行此操作的小型 C 程序:

#include <sys/types.h> 
#include <sys/stat.h> 
#include <termios.h>
#include <fcntl.h> 
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
    int i;
    for (i = 1; i < argc; i++) {
        int fd = open(argv[i], O_NOCTTY, O_RDONLY);
        if (fd >= 0) {
            int result = tcflush(fd, TCIFLUSH);
            close(fd);
            if (result == -1) {
                fprintf(stderr, "%s: Couldn't open file %s; %s\n",
                        argv[0], argv[i], strerror(errno));
                exit (EXIT_FAILURE);
            }
        } else {
            fprintf(stderr, "%s: Couldn't open file %s; %s\n",
                    argv[0], argv[i], strerror(errno));
            exit (EXIT_FAILURE);
        }
    }
}

使用gcc -o tcflush tcflush.c編譯並使用tcflush /dev/tty01

我不確切知道“Linux 中的串行接口文件”是什么。 但我認為這是一個簡單的文件,它總是附加一些文本,並且您想等待附加的新內容而不是從頭開始讀取整個文件。 您可以使用RandomAccessFile類的seek(long)方法來跳過數據。 或者您可以在到達文件末尾時睡一會兒。

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream("src/file.txt");
    int i = 0;
    while (i < 50) { // read only 50 bytes
        byte b = (byte)fis.read();

        if (b == -1) { // end of file, wait
            Thread.sleep(500L);
            continue;
        }
        System.out.print((char) b);
        i++;
    }
    fis.close();
}

這只是一個簡單的例子。 我最多只能讀取 50 個字節,但您想要讀取的內容遠不止這些。 也許你可以使用超時。

外部板只是不斷發送。

這看起來更像是事件驅動方法的目標,而不是通常的順序讀取。

您是否考慮過使用RXTXjSSC 您可以在 Arduino IDE 源代碼中找到一個示例: SerialMonitorSerial

從代碼中您的目標非常不清楚。 我想你想要一些像 linux 中的 tail 命令這樣的功能..如果是這樣,下面的代碼很有幫助..請運行它並檢查它..

import java.io.*;

public class FileCheck {

    static long sleepTime = 1000 * 1;
    static String file_path = "/dev/ttyO1";

    public static void main(String[] args) throws IOException {
            BufferedReader input = new BufferedReader(new FileReader(file_path));
            String currentLine = null;
            while (true) {
                if ((currentLine = input.readLine()) != null) {
                    System.out.println(currentLine);
                    continue;
                }
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
            input.close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM