我正在试图弄清楚如何连续读取文件,一旦添加了新行,就输出该行.我正在使用一个睡眠线程,但它似乎只是吹过整个文件并退出程序.
我有什么错误吗?
这是我的代码:
import java.io.*;
import java.lang.*;
import java.util.*;
class jtail {
public static void main (String args[])
throws InterruptedException, IOException{
BufferedReader br = new BufferedReader(
new FileReader("\\\\server01\\data\\CommissionPlanLog.txt"));
String line = null;
while (br.nextLine ) {
line = br.readLine();
if (line == null) {
//wait until there is more of the file for us to read
Thread.sleep(1000);
}
else {
System.out.println(line);
}
}
} //end main
} //end class jtail
Run Code Online (Sandbox Code Playgroud)
提前致谢
更新:我已经改变了行"while(br.nextLine){"只是"while(TRUE){"
我有一个充满滚动日志文件的目录,我希望能够使用tail.
这些文件的名称如下:
name modified
00A.txt Dec 27 19:00
00B.txt Dec 27 19:01
00C.txt Dec 27 19:02
00D.txt Dec 27 19:03
Run Code Online (Sandbox Code Playgroud)
在一个较旧的unix系统上,我试图想出一个shell脚本,它将在最近修改的特定目录中修改文件,如果该文件被管理关闭(滚动到下一个文件),我想让程序自动开始拖尾新文件,我不必打破尾巴重新运行.
tail -100f `ls -t | head -1`
Run Code Online (Sandbox Code Playgroud)
给定上面的文件名,所需的行为将如下所示:
./logtailer.sh
Run Code Online (Sandbox Code Playgroud)
然后脚本将开始拖尾00D.txt.一旦记录器完成写入00D.txt并且最新的日志文件现在命名为00E.txt,程序将自动开始拖尾该文件.
可以通过观察文件"文件管理已关闭"的尾部输出然后再次运行以下命令来编写此脚本.
tail -100f `ls -t | head -1`
Run Code Online (Sandbox Code Playgroud)
有没有比通过观察"文件管理关闭"文本更优雅的方式?我怎样才能在shell脚本中逐行读取尾部的输出?
编辑:我应该解释一下尾部的-F标志对我来说不是一个选项.它使用不同版本的尾部,不包含此功能.操作系统版本 - Solaris 10
我需要做以下事情以确保我的应用程序服务器
有没有办法在while循环中包含超时?
假设我有一个包含任意行数的文件,比方说125.我想获得除第一个n之外的所有行,比方说,20.所以,我想要第21-125行.
有没有办法用tail
/ head
或其他工具做到这一点?
我想在Java应用程序中"尾随-f"很多日志文件.
我通过监视大小和上次更新并重复打开文件并在文件大小或上次更新时间发生变化时读取最后几个字节 - 然后立即关闭它来实现此功能.
这似乎有问题,因为当记录器决定重命名文件时,我可能会打开它,这会导致某种问题.
我还想检测一个"Rolled"文件,其机制更加确定,而不是注意到文件大小减少......似乎容易出错,但不太可能.
由于我似乎无法访问文件描述符或其他低级文件实用程序,因此我可能无法重现尾部的行为 - 但是有没有任何技巧来读取文件而不将其"锁定"以进行重命名/删除( Windows 7的)
我想另一种可能性是实际产生一个尾部-f进程并读取进程输出,但这看起来有点沉重 - 我在这里扫描了60个日志文件,其中一些有很多输出(大部分将是空闲的).
如何使用powershell来拖尾特定的Windows事件日志?可能吗?
我想说一个文件的输出行5-10,作为传入的参数.
我怎么能使用head
,并tail
做到这一点?
其中,firstline = $2
和lastline = $3
和filename = $1
.
运行它应该如下所示:
./lines.sh filename firstline lastline
Run Code Online (Sandbox Code Playgroud) 假设我在这里有这个代码:
do_recv_loop(State) ->
receive
{do,Stuff} ->
case Stuff of
one_thing ->
do_one_thing(),
do_recv_loop(State);
another_thing ->
do_another_thing(),
do_recv_loop(State);
_ ->
im_dead_now
end
{die} -> im_dead_now;
_ -> do_recv_loop(State)
end.
Run Code Online (Sandbox Code Playgroud)
现在,理论上这是尾递归的,因为对do_recv_loop的三次调用都不需要返回任何内容.但是,erlang会认识到这是尾递归并适当优化吗?我担心嵌套结构可能使它无法识别它.
我已经使用Ubuntu 11.10了一个多星期了.但是一段时间后,当我试图访问我的RoR项目中的日志时,我遇到了这个错误(标题中的那个).我找到了一个修复方法,即在终端中粘贴它:
sudo sysctl -w fs.inotify.max_user_watches = 16384
问题是我必须每天一次又一次地这样做.有谁知道如何在启动时执行此操作?或者有人知道任何永久解决方案?非常感谢!!!
Tail有以下选择:
-f The -f option causes tail to not stop when end of file is reached, but rather to wait for additional data to be appended to the
input. The -f option is ignored if the standard input is a pipe, but not if it is a FIFO.
Run Code Online (Sandbox Code Playgroud)
我只想something
在尾部输出中进行grep .
tail -f <FILE> | grep <SOMETHING>
Run Code Online (Sandbox Code Playgroud)
问题是它只运行一次grep并完成.没有其他输出发生.如何让grep正确运行-f
?