- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我知道 linux 可以安装在物理内存较少的机器上。有没有办法使用 1GB RAM 读取 4GB 日志文件?基本上我想从大日志文件中读取几行。该行可以位于大型日志文件中的任何位置。-如果有人可以帮助我理解 linux tail 实现,我可以在 java 中尝试相同的实现。- 至少我应该使用哪个 java I/O 包来按需读取行,而不是将完整的文件加载到物理内存上,然后逐行读取。
最佳答案
我想你可以使用这样的东西..!!!
/**
* TODO Description go here.
* @author Rajakrishna V. Reddy
* @version 1.0
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public abstract class FileTailer extends AbstractFileNotificationListener
{
/** The logger to log the debugging messages as application runs. */
private final Logger logger;
/** The tail text. */
private final StringBuilder tailText = new StringBuilder(256);
/** The file watcher. */
private final FileWatcher fileWatcher;
/**
* Instantiates a new file tailer.
*
* @param logFileName
* the log file name
* @throws FileNotFoundException
* the file not found exception
*/
public FileTailer(String logFileName) throws FileNotFoundException
{
logger = Logger.getLogger(FileTailer.class);
fileWatcher = FileWatcher.getFileWatcher();
fileWatcher.registerFileNotificationListener(this, logFileName);
fileWatcher.start();
}
/*
* (non-Javadoc)
*
* @see
* com.mt.filewatcher.listener.AbstractFileNotificationListener#onModifyFile
* (com.mt.filewatcher.info.FileInfo, com.mt.filewatcher.info.FileInfo)
*/
@Override
public final synchronized void onModifyFile(FileInfo oldFileInfo, FileInfo newFileInfo)
{
tailText.setLength(0);
final String name = oldFileInfo.getAbsolutePath();
RandomAccessFile randomFile = null;
try
{
randomFile = new RandomAccessFile(new File(name), "r");
final long offset = getOffset(oldFileInfo, newFileInfo);
if (offset > 0)
{
randomFile.seek(offset-1); // use native seek method to skip bytes
}
byte[] bytes = new byte[1024];
int noOfRead = -1;
while ((noOfRead = randomFile.read(bytes)) > 0) // Best approach than reading line by line.
{
for (int i = 0; i < noOfRead; i++)
{
tailText.append((char)bytes[i]);
}
bytes = new byte[1024];
}
final String tailedText = tailText.toString();
tail(tailedText);
if (tailedText != null && tailedText.contains("\n"))
{
final String[] lines = tailedText.split("\n");
for (String line : lines)
{
tailLineByLine(line);
}
}
else
{
tailLineByLine(tailedText);
}
}
catch (IOException e)
{
logger.error("Exception in tailing: ", e);
}
finally
{
if (randomFile != null)
{
try
{
randomFile.close();
}
catch (IOException e)
{
logger.error("Exception in closing the file: ", e);
}
}
}
}
/**
* Gets the tailed text.
* @see #tailLineByLine(String)
*
* @param tailedText
* the tailed text
*/
public abstract void tail(String tailedText);
/**
* Gets the line by line of the tailed text of the file as it is appending
* into file.
* <br><b>Note: This line does not contain the new line character.</b>
*
* @param tailedLine
* the tailed line
*/
public void tailLineByLine(String tailedLine)
{
//TODO
}
/**
* Gets the offset of the given old FileInfo.
*
* @param oldFileInfo
* the old file info
* @param newFileInfo
* the new file info
* @return the offset
*/
protected long getOffset(FileInfo oldFileInfo, FileInfo newFileInfo)
{
if (newFileInfo.getSize() < oldFileInfo.getSize())
{
return -1;
}
return oldFileInfo.getSize();
}
/**
* The main method.
*
* @param args
* the args
* @throws FileNotFoundException
* the file not found exception
*/
public static void main(String[] args) throws FileNotFoundException
{
System.setProperty(LoggerConstants.LOG_CLASS_FQ_NAME, ConsoleLogger.class.getName());
//new FileTailer("/krishna/Project/Hyperic/hq-hq/tools/unit_tests").onModifyFile(new FileInfo("/krishna/Project/Hyperic/hq-hq/tools/unit_tests/Raja/hi.txt"), null);
}
}
关于java - 使用 java 的大型日志文件的“Tail -10”实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233950/
我有一个日志文件,想要创建一个网页(可能是Python,但不是严格意义上),该网页的工作方式类似于Unix的“tail -f filename”命令的工作方式(将新的日志行写入文件时显示)。 这样,该
当我为日志文件运行 tail -f 时,logrotate 旋转它但 tail -f 没有停止。它继续在新文件中显示日志。之后我手动尝试; mv my.log my.log.2 touch my.lo
您将如何在 bash 中实现这一点。这是我在面试中被问到的一个问题,我可以想到高级语言的答案,而不是 shell。 据我了解,tail 的真正实现是查找文件末尾,然后向后读取。 最佳答案 主要思想是保
例如: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"hello: (.*)ABC"
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
为什么我不能tail tail 的结果?我可以调用head在序列 tail返回(和其他变体),但 tail在 tail不起作用(在 2017.10 中): > my $list = ; (a b c
A previous question如何讨论 Haskell 表达式的类型 ap zip tail可以翻译成\x -> zip x (tail x)的类型.这很有启发性,但是那里的问题和答案都没有涉
因为我试图保持容器运行,所以我在 docker compose 文件中将“tail -f/dev/null”指定为 command: version: '2' services: serviceN
以下是为拖尾“n”行文件编写的代码。 import java.io.RandomAccessFile; import java.util.HashMap; import java.util.Map
我有一个递归方案样式结构,我想获得所有子结构的列表,包括完整结构 - 即相当于 tails函数在 List 上执行.我认为可以通过调用 para 来实现这一点。 ,在每一步映射回原始结构,然后分别将原
编辑 这是一个 JSFiddle,其中注释掉了“tail”函数的代码。 Solar System JSFiddle 我有一个我正在研究的物体,它有一个围绕中心质量运行的物体。这非常有效。 我现在正尝试
我想跟踪一个文件,但 tail -f 始终从最后 10 行开始。有没有办法输出整个文件然后遵循? 我的目标是查找日志中出现的所有字符串,例如 tail -f Streaming_log | grep“
我试图在我的循环单向链表中将两端连接在一起。在文件名 file.txt 中,包含 ABCDEFGHIJKLMNOPQRSTUVWXYZ 作为文本,我能够分别打印出头部和尾部 A 和 Z。但是,我希望
我目前遇到命令问题 while sleep 0.3; do echo "1"; done | tail -n +3 | grep --line-buffered "1" 我想要一个看起来像这样的输出:
所以我想稍微了解一下 Linux 脚本,并从书中的一个简单示例开始。在这本书中,作者要我从 snort.conf 中获取“第 6 步:配置输出插件”之前的五行。 类似于作者,我确定了我想要的行的位置,
我想跟踪一个将不间断写入的日志文件,问题是在我的脚本中我不想指定行数或字节数等。要跟踪,我想在我的脚本中指定每次跟踪之前未跟踪的最后一行。我怎样才能在我的脚本中做到这一点?谢谢 最佳答案 我认为 20
Problem: Program to read the lines from infinite stream starting from its end of file. #解决方案: import
我想安装 Linux Tails。我已经将 ppa:tails-team/tails-installer 添加到我的源中,但是当我的 Ubuntu 软件中心尝试下载存储库信息时,我收到此错误: W:F
读取记录时间序列数据的 1 GB 文件并生成包含两列(一列时间,另一列数字)的实时图表的最佳方法是什么?我看到您有不同的方式来调整文件。 最佳答案 对于 RRDTool 来说听起来不错. 但如果您想坚
需要监视日志文件中的特定字符串“Server running at http”。在日志文件中找到该字符串后,我需要停止检查并想继续其余代码。 目前我正在使用 "tail -f my-file.log
我是一名优秀的程序员,十分优秀!