gpt4 book ai didi

java - 使用 java 的大型日志文件的“Tail -10”实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:00:56 27 4
gpt4 key购买 nike

我知道 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com