Skip to main content

FileWriter.writeln(String)

7 replies [Last post]
monika_krug
Offline
Joined: 2004-10-14

I would like a method in FileWriter that adds the system-dependent linebreak character after the line: .writeln(String). As FileWriter inherits its write methods from OutputStreamWriter and Writer, it should probably be defined in one of these.

Yeah, I could use PrintWriter, but there are more differences to PrintWriter than that it has println methods, e.g. it does not throw Exceptions, but sets an error flag, so I would really prefer if FileWriter had a writeln method.

So far I do write(String + BR) and have static String BR = System.getProperty("line.separator"); in the class. It's okay, but a writeln method would be nicer.

Monika.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ronaldtm
Offline
Joined: 2003-07-18

What about PrintWriter?

monika_krug
Offline
Joined: 2004-10-14

I mentioned in my original post why I do not want to use it.

Monika.

patrikbeno
Offline
Joined: 2004-10-11

I think it is much better to use BufferedWriter which has newLine() method doing what you need. And it performs much better of course. However, println() still missing :-(

Anyway, I would prefer some implementation that behaves the similar way as StringBuffer or StringBuilder and allows chained calls (append().append().append()...)

jarouch
Offline
Joined: 2004-03-04

Yes, and use this 'pattern' anywhere it is possible, for example Collection's add().add() ... Map's put().put()... etc.

mthornton
Offline
Joined: 2003-06-10

No, you should wrap FileWriter in a BufferedWriter which already has a newLine() method. Perhaps some writeln convenience methods should be added to BufferedWriter.

monika_krug
Offline
Joined: 2004-10-14

So, add writeln to java.io.Writer, then both BufferedWriter and FileWriter will have it.

Monika.

patrikbeno
Offline
Joined: 2004-10-11

Have you noticed that since 1.5 Writer has mentioned chained method [i]Writer append(CharSequence csq)[/i] & friends?

Still, java.io.Writer is the best place for something like writeln() or newLine()...