O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required