Article View: borland.public.jbuilder.java.language
Article #17275Re: Problems writing newlines to a file
From: John P. McGrath
Date: Tue, 24 Nov 1998 00:00
Date: Tue, 24 Nov 1998 00:00
57 lines
2367 bytes
2367 bytes
What is happening is that you are writing just a single ASCII LF
character in the file, since the '\n' escape sequence indicates the LF
character.
Your problem is due to the fact that different operating systems
handle end of lines differently. In general, they embed a character
(or characters) to indicate the end of line, but the character is
different on different platforms:
Unix: LF
Dos/Windows: CR + LF
Macintosh: CR
Most programs will handle alternate forms of line enders, but as
usual, Microsoft is both out of step and obstinate. They insist on
using the 2 character CR/LF combination as a line ender (do they think
we are still using typewriters?), and their programs, such as Notepad,
refuse to recognize the single LF (or CR) variant. (Do they think
that the LF character means draw a box?)
Rather than use the '\n' character explicitly, you should either use
the BufferedWriter newLine() method, or one of the various println or
writeln methods in the java.io package, or write the value of the
system property "line.separator".
--
Regards,
John P. McGrath
<mcgrath@enter.net>
<jpm@nscs.fast.net>
Matt Butler <butler@cyberramp.net> wrote:
> I am having an problem with writing new lines to a file. I have tried
> several different things, but to give you an idea here is the latest code I
> tried:
>
> String s = new String("test\n");
> char[] buf = new char[s.length()];
> s.getChars(0,s.length(),buf,0);
> Writer out = new OutputStreamWriter(new
> FileOutputStream("D:\\WWW\\Html\\User3.rpt"));
> out.write(buf,0,s.length());
> out.flush();
>
> What is happening is that the \n is coming out as an unprintable character
> rather than a newline. It works fine if sent to system.out rather than a
> file.
>
> Any ideas?
>
> Thanks,
> Matt
Technical Details
Message-ID:
<MPG.10c454ec12f0473a989734@forums.inprise.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<72sc3h$14a13@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,1b6563995c977abb X-Google-Attributes: gidf74b3,public From: mcgrath@enter.net (John P. McGrath) Subject: Re: Problems writing newlines to a file Date: 1998/11/24 Message-ID: <MPG.10c454ec12f0473a989734@forums.inprise.com> X-Deja-AN: 415124576 References: <72sc3h$14a13@forums.borland.com> Organization: Software Consulting Services Newsgroups: borland.public.jbuilder.java.language