I am writing a class that will read lines from a log file when it is updated.
I am using Apache VFS2 to call a method when updating a file. My main problem is that I don’t want to read a line from a file if the line is not finished yet, because it has a character like the line separator \ "\ n" or "\ r" at the end. I think I looked at all the Java libraries that I can read strings, but they all discard information about EOF and line termination, so I don't think I can use them.
Instead, I look at it in bytes byte, and then check the result to then discard everything that comes after the last line separator. I was wondering what you think of the best method for this.
So for example:
2013-Jul-01_14:07:17.875 - Connection to Message Bus is reestablished<LF> 2013-Jul-01_14:07:17.875 - Connection to Message Bus is reestablished<LF> 2013-Jul-01_14:15:08.205 - No connection to Message Bus - reestablish before we can publish<LF> 2013-Jul-01_14:15:08.205 - NOT A REAL LINE PLEASE DONT READ
I want to read in the first 3, but not the fourth, since it does not have a line or a carriage return () character.
I looked at the Apache commons-io Tailer, but I can’t say that it will give me “incomplete” lines (and I understand that I will have to cut out the VFS2 stuff to use it).
So psudo-code:
private void ingestFileObject(FileObject file) { BufferedInputStream bs = new BufferedInputStream(file.getContent().getInputStream()); StringBuilder result = new StringBuilder(); while (bs.available() > 0) { result.append((char) bs.read()); } bs.close(); String resultString = result.toString();
Or any other solutions that completely ignore my psudo code are welcome at this point too ...
thanks
source share