gpt4 book ai didi

java - 查找具有最新信息的文件

转载 作者:行者123 更新时间:2023-12-01 14:57:21 25 4
gpt4 key购买 nike

我有一个日志文件列表,我需要找到哪个日志文件具有特定行的最新版本,并且所有或没有一个都可能具有该行。

文件中的行如下所示:

2013/01/06 16:01:00:283  INFO ag.doLog: xxxx xxxx xxxx xxxx

我需要一条线,可以说

xx/xx/xx xx:xx:xx:xxx  INFO ag.doLog: the line i need

我知道如何获取文件数组,如果我向后扫描,我可以在每个文件中找到最新的最新行(如果存在)。

最大的问题是文件可能很大(2k 行?),并且我想以相对快速的方式(几秒钟)找到该行,所以我愿意征求建议。

个人想法:如果文件在 X 时间有该行,则任何在 X 时间之前未找到该行的文件都不应再被扫描。这将需要同时搜索所有文件,我不知道如何进行。

Atm 代码损坏,我想可能是内存不足。

代码:

if(files.length>0)  {  //in case no log files exist
System.out.println("files.length: " + files.length);
for(int i = 0; i < files.length; i++) { ///for each log file look for string
System.out.println("Reading file: " + i + " " + files[i].getName());
RandomAccessFile raf = new RandomAccessFile(files[i].getAbsoluteFile(), "r"); //open log file
long lastSegment = raf.length(); //Finds how long is the files
lastSegment = raf.length()-5; //Sets a point to start looking
String leido = "";
byte array[] = new byte[1024];
/*
* Going back until we find line or file is empty.
*/
while(!leido.contains(lineToSearch)||lastSegment>0) {
System.out.println("leido: " + leido);
raf.seek(lastSegment); //move the to that point
raf.read(array); //Reads 1024 bytes and saves in array
leido = new String(array); //Saves what is read as a string
lastSegment = lastSegment-15; //move the point a little further back
}
if(lastSegment<0) {
raf.seek(leido.indexOf(lineToSearch) - 23); //to make sure we get the date (23 characters long) NOTE: it wont be negative.
raf.read(array); //Reads 1024 bytes and saves in array
leido = new String(array); //make the array into a string
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(leido.substring(0, leido.indexOf(" INFO "))); //get only the date part
System.out.println(date);
//if date is bigger than the other save file name
}
}
}

最佳答案

我发现代码很难验证。人们可以将任务拆分为向后读取器,该读取器从文件末尾读取行。并使用它来逐行解析日期。

请注意,我不是想要漂亮的代码,而是像这样的东西:

public class BackwardsReader implements Closeable {

private static final int BUFFER_SIZE = 4096;

private String charset;
private RandomAccessFile raf;
private long position;
private int readIndex;
private byte[] buffer = new byte[BUFFER_SIZE];

/**
* @param file a text file.
* @param charset with bytes '\r' and '\n' (no wide chars).
*/
public BackwardsReader(File file, String charset) throws IOException {
this.charset = charset;
raf = new RandomAccessFile(file, "r");
position = raf.length();
}

public String readLine() throws IOException {
if (position + readIndex == 0) {
raf.close();
raf = null;
return null;
}

String line = "";
for (;;) { // Loop adding blocks without newline '\n'.

// Search line start:

boolean lineStartFound = false;
int lineStartIndex = readIndex;
while (lineStartIndex > 0) {
if (buffer[lineStartIndex - 1] == (byte)'\n') {
lineStartFound = true;
break;
}
--lineStartIndex;
}
String line2;
try {
line2 = new String(buffer, lineStartIndex, readIndex - lineStartIndex,
charset).replaceFirst("\r?\n?", "");
readIndex = lineStartIndex;
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(BackwardsReader.class.getName())
.log(Level.SEVERE, null, ex);
return null;
}
line = line2 + line;
if (lineStartFound) {
--readIndex;
break;
}

// Read a prior block:

int toRead = BUFFER_SIZE;
if (position - toRead < 0) {
toRead = (int) position;
}
if (toRead == 0) {
break;
}
position -= toRead;
raf.seek(position);
raf.readFully(buffer, 0, toRead);
readIndex = toRead;
if (buffer[readIndex - 1] == (byte)'\r') {
--readIndex;
}
}
return line;
}

@Override
public void close() throws IOException {
if (raf != null) {
raf.close();
}
}
}

还有一个使用示例:

public static void main(String[] args) {
try {
File file = new File(args[0]);
BackwardsReader reader = new BackwardsReader(file, "UTF-8");
int lineCount = 0;
for (;;) {
String line = reader.readLine();
if (line == null) {
break;
}
++lineCount;
System.out.println(line);
}
reader.close();
System.out.println("Lines: " + lineCount);
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}

关于java - 查找具有最新信息的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14185198/

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