Read full lines from a file that is constantly updated.

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(); //determine what part of resultString is after last carriage return/line seperate (using regex [\\r\\n]+? //remove the offending part of String. } 

Or any other solutions that completely ignore my psudo code are welcome at this point too ...

thanks

+4
source share
2 answers

Here is what I did:

  BufferedReader bufReader = new BufferedReader(new InputStreamReader(file.getContent().getInputStream())); StringBuilder result = new StringBuilder(); int readInInt = -1; String charsSinceLastLineSep = ""; if (bufReader.ready()) { while (-1 != (readInInt = bufReader.read())) { char readInChar = (char) readInInt; // if new line reset line buffer, otherwise add to buffer if (readInChar == '\n' || readInChar == '\r') { charsSinceLastLineSep = ""; } else { charsSinceLastLineSep += readInChar; } result.append(readInChar); } bufReader.close(); // remove all characters added since last Carriage Return or NewLine was found indicating // that line was not a complete log line String resultString = (result.subSequence(0, (result.length() - charsSinceLastLineSep.length())).toString()); 
+1
source

Does Scanner help?

  Scanner scanner = new Scanner(file); //block till there is some thing with a new line while (scanner.hasNextLine()) { String line = scanner.nextLine(); //do processing. } 
+1
source

Source: https://habr.com/ru/post/1491564/


All Articles