Reading “Continued” Lines

Problem

You need to read lines that are continued with backslashes (\) or that are continued with leading spaces (such as email or news headers).

Solution

Use my IndentContLineReader or EscContLineReader classes.

Discussion

This functionality is likely to be reused, so it should be encapsulated in general-purpose classes. I offer the IndentContLineReader and EscContLineReader classes. EscContLineReader reads lines normally, but if a line ends with the escape character (by default, the backslash), then the escape character is deleted and the following line is joined to the preceding line. So if you have lines like this in the input:

Here is something I wanted to say:\
Try and Buy in every way.
Go Team!

and you read them using an EscContLineReader’s readLine( ) method, then you will get the following lines:

Here is something I wanted to say: Try and Buy in every way.
Go Team!

Note in particular that my reader does provide a space character between the abutted parts of the continued line. An IOException will be thrown if a file ends with the escape character.

IndentContLineReader reads lines, but if a line begins with a space or tab, that line is joined to the preceding line. This is designed for reading email or Usenet news (“message”) header lines. Here is an example input file:

From: ian Tuesday, January 1, 2000 8:45 AM EST To: Book-reviewers List Received: by darwinsys.com (OpenBSD 2.6) from localhost at Tuesday, January 1, 2000 8:45 AM EST Subject: Hey, ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.