Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Download any one of jEdit, Notepad++ and Notepad2 and use them for programming in preference to Notepad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic