Java 扫描仪不会跟踪文件

发布于 2024-08-31 07:11:03 字数 1083 浏览 3 评论 0原文

尝试跟踪/解析一些日志文件。条目以日期开头,然后可以跨越多行。

这可行,但不会看到要归档的新条目。

File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");

while (true) {
    while(src.hasNext()){
    System.out.println("[ " + src.next() + " ]");
    }
}

扫描仪的 next() 或 hasNext() 似乎没有检测到文件的新条目。

知道我还能如何实现,基本上,带有自定义分隔符的 tail -f 。


好的 - 根据凯利的建议,我正在检查&刷新扫描仪,这有效。谢谢 !!

如果有人有改进建议,请提出!

File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");

while (true) {
    while(src.hasNext()){
    System.out.println("[ " + src.next() + " ]");
    }

    Thread.sleep(50);
    if(bis.available() > 0){
    src = new Scanner(bis);
    src.useDelimiter("\n2010-05-01 ");
    }
}

Trying to tail / parse some log files. Entries start with a date then can span many lines.

This works, but does not ever see new entries to file.

File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");

while (true) {
    while(src.hasNext()){
    System.out.println("[ " + src.next() + " ]");
    }
}

Doesn't seem like Scanner's next() or hasNext() detects new entries to file.

Any idea how else I can implement, basically, a tail -f with custom delimiter.


ok - using Kelly's advise i'm checking & refreshing the scanner, this works. Thank you !!

if anyone has improvement suggestions plz do!

File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");

while (true) {
    while(src.hasNext()){
    System.out.println("[ " + src.next() + " ]");
    }

    Thread.sleep(50);
    if(bis.available() > 0){
    src = new Scanner(bis);
    src.useDelimiter("\n2010-05-01 ");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-09-07 07:11:04

我猜测扫描仪正在解析已缓冲的bis,但缓冲区永远不会刷新。您可能依赖 BufferedInputStream 或 Scanner 来继续从流中读取字节,但我认为您必须自己这样做。

来自 Java 文档:

BufferedInputStream 添加
另一个输入的功能
流——即缓冲的能力
输入并支持标记和
重置方法。当
BufferedInputStream 被创建,
创建内部缓冲区数组。作为
从流中读取字节或
跳过,内部缓冲区是
根据需要重新填充
包含输入流,许多字节位于
一次。标记操作会记住一个
输入流中的点和
复位操作会导致所有字节
从最近的标记开始读取
新操作之前要重读
字节是从包含的
输入流。

I would guess that the Scanner is parsing bis which is buffered but the buffer is never getting refreshed. You might be relying on the BufferedInputStream or the Scanner to keep reading bytes from the stream but I think you have to do that yourself.

From the Javadocs:

A BufferedInputStream adds
functionality to another input
stream-namely, the ability to buffer
the input and to support the mark and
reset methods. When the
BufferedInputStream is created, an
internal buffer array is created. As
bytes from the stream are read or
skipped, the internal buffer is
refilled as necessary from the
contained input stream, many bytes at
a time. The mark operation remembers a
point in the input stream and the
reset operation causes all the bytes
read since the most recent mark
operation to be reread before new
bytes are taken from the contained
input stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文