Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Carriage return in an output stream

666 views
Skip to first unread message

Greg Johnson

unread,
Jan 11, 2001, 4:15:18 AM1/11/01
to
2 questions:

a) I'm having difficulty finding the best way to output text to a file (to
create an html document). I have discovered there are several ways to do
this... which is the best?

I read that FileWriter is one of the best ways to do this.


b) How do I write a newline character? the standard "obj.write('\n');"
writes a special character. Or at least, my non-unicode(?) MS Notepad sees
a special character. Makes the source code a little messy.

Greg Johnson
gdbjo...@hotmail.com


mattbu...@my-deja.com

unread,
Jan 11, 2001, 1:06:30 PM1/11/01
to
a) FileWriter is fine, but I would recommend wrapping the FileWriter in
a BufferedWriter for better performance.
b) If you opened the file on Unix/Linux/Solaris, you wouldn't see the
funny character because the \n character is the line-separator for
these platforms. For Mac it is \r, and for Windows it is \r\n combined.
Notepad, when it sees the 2-character \r\n combination, "eats" them
both and displays a line return. When it encounters \n by itself it
gets confused and just displays a block character. Best solution would
be
String lineSep = System.getProperty("line.separator")
then instead of appending, append lineSep b/c it will always write the
correct characters based on the platform on which the app is currently
running.

Matt

In article <GQe76.44639$n%.2346476@news20.bellglobal.com>,


Sent via Deja.com
http://www.deja.com/

Dale King

unread,
Jan 11, 2001, 1:52:35 PM1/11/01
to
"Greg Johnson" <gjo...@po-box.mcgill.ca> wrote in message
news:GQe76.44639$n%.2346476@news20.bellglobal.com...

>
> I read that FileWriter is one of the best ways to do this.

Actually, I think FileWriter is a stupid class that should not exist. It
hides the character encoding issues by only supporting the platform default
encoding. If you look at the source for FileWriter all it has are
constructors. The expression:

new FileWriter( foo )

is simply shorthand for

new OutputStreamWriter( new FileOutputStream( foo ) )

with the exception that with the OutputStreamWriter you can also specify an
encoding.
--
Dale King

David Solum

unread,
Jan 11, 2001, 2:08:17 PM1/11/01
to
> String lineSep = System.getProperty("line.separator")
> then instead of appending, append lineSep b/c it will always write the
> correct characters based on the platform on which the app is currently
> running.

Or, if you do use a BufferedWriter, call the newLine() method.

Here's the Sun documentation for this method:

"A newLine() method is provided, which uses the platform's own notion of
line separator as defined by the system property line.separator. Not all
platforms use the newline character ('\n') to terminate lines. Calling this
method to terminate each output line is therefore preferred to writing a
newline character directly."

JAG

unread,
Jan 11, 2001, 2:47:36 PM1/11/01
to Greg Johnson
Use FileWriter if it works for you.

Use a text editor for viewing files. NotePad is not a text editor. There are
many editors available. some are open source. I just "discovered" jEdit. I
think a more user-friendly and less powerful emacs.

Regards,
Frank

0 new messages