Line Breaks

As previously mentioned, the println( ) method always adds a line break at the end of each line it prints. You can even call println( ) with no arguments to print just a line break:

public void println( )

The line break character varies from platform to platform. In particular:

  • On Unix (including Mac OS X), it's a linefeed, , ASCII 10.
  • On Mac OS 9, it's a carriage return, , ASCII 13.
  • On Windows, it's a carriage return linefeed pair, , ASCII 13 followed by ASCII 10.

This is almost never what you actually want!

Most file formats and most network protocols care a great deal about which line break character is written.[*] For instance, if you're writing a web client or server, the HTTP specification requires that header lines end with carriage return linefeed pairs. It doesn't matter whether the client or server is a Mac, a PC, a Unix workstation, or a Palm Pilot. It must use as the line break. You can specify this by explicitly passing the line break you want to the print( ) method rather than calling println( ). For example:

[*] XML is a notable exception here. It treats linefeeds, carriage returns, and carriage return linefeed pairs equally.

for (int i = 0; i <= 127; i++) {

In practice, most HTTP servers and clients accept requests that use the wrong line breaks. However, some aren't so forgiving, and you really shouldn't count on this behavior.

If for some reason you want to know which line break character will be used, the line.separator system property will tell you:

String lineBreak = System.getProperty("line.separator");

Not all line breaks are created equal. If the PrintStream is set to autoFlushthat is, if the second argument to the constructor is trueafter every call to println( ) and after every linefeed that's printed, the underlying stream will be flushed. Thus, out.println( ) and out.print(" ") both flush the stream. So does out.print(" "), because it contains a linefeed. However, out.print(" ") does not cause an automatic flush.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

show all menu

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: