aspose file tools*
The moose likes Beginning Java and the fly likes [newbie] appending a line feed (CR) when writing to a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] appending a line feed (CR) when writing to a text file " Watch "[newbie] appending a line feed (CR) when writing to a text file " New topic
Author

[newbie] appending a line feed (CR) when writing to a text file

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

I'm trying to write to an existing log file, and I understand (by reading a few posts) that there is no way to append to an existing text file.



This is how my text file looks like:
http://cid-b712073b3513eb8e.skydrive.live.com/self.aspx/.Public/text%20file.png

As a workaround I guess I could always use a pipe (|) and parse the file if I need it.

Ooops... the problem was Notepad It wasn't reading the CRs somehow. Should have known better

Well, not quite. I'm still unsure about how to declare a Unicode character as a char type. The single quotation marks don't seem to compile:

Err:
C:\Users\Administrator\Desktop\test.java:57: illegal line end in character literal
char cr = '\u000d';
^
1 error

Tool completed with exit code 1

Jon
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

OK, a few points.

1. Although 'udddd' is a valid way to specify unicode characters, the two exceptions are carriage returns ('\u000d') and line feeds ('\u000a'). These should be specified as '\r' and '\n' respectively.

2. You shouldn't even do that. Instead, support platform independence by asking for the platform's line separator sequence:
String newline = System.getProperty("line.separator");

(It's a string instead of a character in case multiple characters are needed to separate the lines, say both CR and LF. The only platform I know of that actually does that is an OS developed by a madman and his cronies up in Redmond, Washington. Still you never know who might be using it.)

3. You absolutely can append to a file. In fact, FileWriter has a constructor that takes a boolean append parameter. However, for a log file you could have multiple simultaneous writers, which is going to cause problems.

4. log4j is a nice free framework for logging that's solved all these problems for you. Java, from 1.4 on, borrows many of these ideas directly into the API. I still prefer log4j though.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18903
    
    8

Greg Charles wrote:1. Although 'udddd' is a valid way to specify unicode characters, the two exceptions are carriage returns ('\u000d') and line feeds ('\u000a'). These should be specified as '\r' and '\n' respectively.


Yup, absolutely. And if you want the gritty details of why: when the compiler goes through your source code, the first thing it does, before it gets down to any actual parsing, is to replace Unicode escapes by the actual Unicode characters they represent. So your little example looks like this to the compiler:

And now you should be able to see what that error message was all about.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
This is what I tried to post last night, but something went wrong and it never appeared.

**************************************************************************************************************************************
I was quite surprised to see "CR" in the title of this thread. I wondered what I had done. Then I read it properly.

CR is not a "linefeed." "Linefeed" is the correct name for what we always call newline (\n) ((char)0x0a) and \r ((char)0x0d) is carriage return also called CR. Oddly enough I was writing about line ends ages ago (well 20 minutes ago): here.

Go through the FileWriter constructors, and you find a way of appending text to a text file. It is quite simple. Go through the methods of the BufferedWriter class, and it tells you how to end lines.

I would usually suggest using the Formatter class for writing to a text file, but I have never worked out how to make a Formatter append to a file, so you will have to use BufferedWriter and FileWriter. It might be possible with the Appendable interface; don't know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

And for the record, my failed post:
----
Jon Camilleri wrote:I'm trying to write to an existing log file, and I understand (by reading a few posts) that there is no way to append to an existing text file.

Sure there is. Just check all of the FileWriter constructors. FileOutputStream has the same feature.


how about \r\n? Or even better: System.getProperty("line.separator");

Ooops... the problem was Notepad It wasn't reading the CRs somehow. Should have known better

Notepad has problems with all line ending sequences that are not exactly \r\n. That includes \r and \n.

Well, not quite. I'm still unsure about how to declare a Unicode character as a char type. The single quotation marks don't seem to compile:

Err:
C:\Users\Administrator\Desktop\test.java:57: illegal line end in character literal
char cr = '\u000d';
^
1 error

Tool completed with exit code 1

The problem is that unicode sequences are actually interpreted by the compiler. '\u000d' is a carriage return, so the compiler sees the source code as this:
Similarly, '\u000A' is also not allowed - that's a line break. That's what \r and \n are for.

---
Guess most of us had the same idea


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Rob Prime wrote:And for the record, my failed post:
----
Jon Camilleri wrote:I'm trying to write to an existing log file, and I understand (by reading a few posts) that there is no way to append to an existing text file.

Sure there is. Just check all of the FileWriter constructors. FileOutputStream has the same feature.


how about \r\n? Or even better: System.getProperty("line.separator");

Ooops... the problem was Notepad It wasn't reading the CRs somehow. Should have known better

Notepad has problems with all line ending sequences that are not exactly \r\n. That includes \r and \n.

Well, not quite. I'm still unsure about how to declare a Unicode character as a char type. The single quotation marks don't seem to compile:

Err:
C:\Users\Administrator\Desktop\test.java:57: illegal line end in character literal
char cr = '\u000d';
^
1 error

Tool completed with exit code 1

The problem is that unicode sequences are actually interpreted by the compiler. '\u000d' is a carriage return, so the compiler sees the source code as this:
Similarly, '\u000A' is also not allowed - that's a line break. That's what \r and \n are for.

---
Guess most of us had the same idea


Yeah I mean I was wondering why I was simply looking at the same file in Notepad and Textpad and saying ....what do Textpad and the Eclipse console do that Notepad does not like?

I had forgotten that a few years ago I had actually came across (and probably written) a code snippet in C# - *eyes down and backing away slowly*) that illustrated CR+LF.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> output.write( _s + '\r'); //this bugger does not work either

perhaps using a PrintWriter and its println() might work better for you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Download any one of jEdit, Notepad++ and Notepad2 and use them for programming in preference to Notepad.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [newbie] appending a line feed (CR) when writing to a text file