Reputation: 499
I want to use java to read a weblogic log file while weblogic is writing log into it (buffered),but I only want read content that is present when I start to read it.
how can I do this ?
public class DemoReader implements Runnable{
public void run() {
File f = new File ("c:\\test.txt");
long length = f.length();
long readedBytes = 0;
System.out.println(length);
try {
BufferedReader fr = new BufferedReader(new FileReader(f));
String line = "";
while((line = fr.readLine()) != null && readedBytes < length){
readedBytes += line.getBytes().length;
if(readedBytes > length){
break;
}else{
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 5
Views: 2842
Reputation: 7257
So long as the log file is only locked for write access you should be able to copy it as @karim79 has suggested. After that the copy belongs to you so you can do whatever you like with it.
Here is some code that should achieve what you're after - it just copies the file byte by byte to the System.out stream:
public class Main {
public static void main(String[] args) throws IOException {
// Identify your log file
File file = new File("path/to/your/logs/example.log");
// Work out the length at the start (before Weblogic starts writing again)
long size = file.length();
// Read in the data using a buffer
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
long byteCount=0;
int result;
do {
// Read a single byte
result = bis.read();
if (result != -1)
{
// Do something with your log
System.out.write(result);
} else {
// Reached EOF
break;
}
byteCount++;
} while (byteCount<size);
// Printing this causes a final flush of the System.out buffer
System.out.printf("%nBytes read=%d",byteCount);
bis.close();
is.close();
}
}
And there you go.
Notes on log files
If the log file is enormous (say >1Gb) then you should really consider changing your logging configuration to incorporate a rolling log file which will automatically break the logs down into chunks (say 1Mb) which are more suitable for viewing in shell editors (like vim).
Upvotes: 1
Reputation: 28703
You can get the size of the file at the moment you are starting to read, and then read N
number of bytes (assuming that the file isn't locked by the writer and its content from 0
to N
is not going to be changed).
Upvotes: 3