Search code examples
javanewlinefilestream

Breaking a line with RSCRLF and not just CRLF using Java (1.8)


I am currently working on a file reader (File Input Stream and Buffered Reader) to read lines from a file (This is a proprietary log file from an application and it has CRLF as line feed but RSCRLF as Record Separator. But when I use Java File Readers, the lines are read based on default CR/LF and considered as different lines even though they are of same line till the Record Separator (RS). I started reading the characters one by one (instead of lines) and got a byte[] array of characters. But how do I check for RSCRLF characters and break it at that point? from ASCII table:

RS -> CHAR, 30 -> DEC, 1E -> HEX
CR -> CHAR, 13 -> DEC, D -> HEX
LF -> CHAR, 10 -> DEC, A -> HEX

Any ideas? Not looking for a code but an idea on how to read through bytes and find RSCRLF together to break at that point?

Thanks


Solution

  • You could potentially use a Scanner with a custom delimiter, here's what that would look like:

    ...
    Scanner scanner = new Scanner(readable);
    scanner.useDelimiter("\u001E\r\n");
    while (scanner.hasNext()) {
        String record = scanner.next();
        ...
    }