Reputation: 2026
I'm looking for the least impacting method to read a few different log files. By "least impacting" I mean the read wont affect log rotation, wont cause undue I/O activity, etc. I'm looking to accomplish this with Java...
Google afforded me the following little snippit of code:
FileStream fs = new FileStream("C:\foo.txt",
FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
This should be failry trivial to implement, but, I'm none too sure of my Java "cut and paste" abilities and would like to know if there is a much better way to do this. The big picture here is to troll some apache and tomcat logs looking for a few key entries based on application activity and take some action based on the entry. Anyway, any tips? Thank you!
Upvotes: 1
Views: 411
Reputation: 199215
Pick one from here
:)
I haven't try them all but there are some useful comments from Jon Skeet on how not to do it.
My bet will be for the nio one.
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return CharSet.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
See details in that question.
Upvotes: 1