writing to text file

I'm having trouble with some text file writing stuff. When i try to write the '\n' character for a new line, the only thing that shows up is a black square character.

I don't really like using PrintWriter.println() method for the newline in a text file. Is there a way to get the '\n' char to work?

[317 byte] By [MakerOfJavaa] at [2007-10-1 5:48:19]
# 1

> I don't really like using PrintWriter.println() method for the newline

> in a text file. Is there a way to get the '\n' char to work?

Why not?

Newlines are different on different platforms, Win ues two chars, Unix one of the two, and MacOS the other one.

Use Systemm.getProperty( "line.separator" ) to get yours.

mlka at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 2
the result that i came up with was a null! How can I use that?!
MakerOfJavaa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 3
> the result that i came up with was a null! How can I> use that?!I'd take that as I've mis-spelt something... http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()
mlka at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 4
If you dont like print writer try using PrintStream it has print and println methods
LRMKa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 5
MakerOfJava (Mr. Coffee?):What are you trying to do? Where and in what form are you getting your data? How are you writing it to file and what do you want back from your file?
MartinRineharta at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 6

I am trying to write the newline character into a textfile using

PrintWriter writer = new PrintWriter( new BufferedWriter(new FileWriter) ) )

I am getting the data from a String. I want the lines in the text file to be spaced properly but when I open the file in notepad, all i get is a black square like character where the '\n' character was in the String and not a 'new line', but the strange thing is, when i use type command in MS-DOS, it prints out just fine. I get a proper newline when i do writer.println() but that won't always be easy to use.

MakerOfJavaa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 7

Notepad will look at the file for specific character sequences to denote "lines" (note: there really is no such thing as a "line" - it's all just in the way apps parse the files, to separate the contents into "lines"). That character sequence is a carriage-return ('\r') followed by a line-feed ('\n'). So obviously if Notepad isn't displaying it the way you expected, then you didn't write it to output those sequences. Are you running this on a UNIX platform, and expecting println to output the DOS-style line separators? It won't.

warnerjaa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 8
the '\r\n' worked. I got a newline in notepad. Thanks for all your help.
MakerOfJavaa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 9

> the '\r\n' worked. I got a newline in notepad. Thanks

> for all your help.

But again, if you're running this in Windows, you really just should use println. Next best would be to follow the advice given earlier, regarding System.getProperty("line.separator") - or whatever the actual property is named (you claimed to have received null for that, so look where he pointed you to find the real property name).

warnerjaa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 10

My code

String newline = System.getProperty("line.separator");

StringBuffer str = new StringBuffer();

str.append("<o-ex:rights\r\n" + //+ newline +

"\txmlns:o-ex=\"http://odrl.net/1.1/ODRL-EX\"" + newline +

"\txmlns:o-ex=\"http://odrl.net/1.1/ODRL-DD\"" + newline + ">" + newline);

I'm writing this string to file.

still I'm getting squrebox instead of newline in notepad

PavanaHa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 11

> I'm writing this string to file.

> still I'm getting squrebox instead of newline in

> notepad

You do have an explicit "\r\n" there (after "<o-ex:rights"--the forum Quote Original screwed up your code, so I'm not including that here).

Also, the "//" in the line with "str.append" makes the rest of that line a comment.>

MLRona at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 12
Sorry this is my code. no \r\n no comment.stiil I'm not getting. str.append("<o-ex:rights" + newline +"\txmlns:o-ex=\" http://odrl.net/1.1/ODRL-EX\"" + newline +"\txmlns:o-ex=\" http://odrl.net/1.1/ODRL-DD\"" + newline + ">" + newline);
PavanaHa at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 13
you can use BufferedWriter, that contains a newLine() method: http://java.sun.com/j2se/1.3/docs/api/java/io/BufferedWriter.html
danielkarda at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...
# 14

> My code

>

> String newline =

> System.getProperty("line.separator");

>

> StringBuffer str = new StringBuffer();

> str.append("<o-ex:rights\r\n" + //+ newline +

> "\txmlns:o-ex=\"http://odrl.net/1.1/ODRL-EX\""

> -EX\"" + newline +

> "\txmlns:o-ex=\"http://odrl.net/1.1/ODRL-DD\""

> -DD\"" + newline + ">" + newline);

>

>

> I'm writing this string to file.

> still I'm getting squrebox instead of newline in

> notepad

Hai, i m havng 1 dbt. abt. ur ability to look at api

import java.io.*;

public class NewLine

{

public static void main (String [] pencil)

{

try

{

FileOutputStream fos = new FileOutputStream(new File("blah.txt"));

PrintWriter pw = new PrintWriter(fos, true);

StringBuffer str = new StringBuffer(0);

String newline = System.getProperty("line.separator");

str.append("<o-ex:rights" + newline +

"\txmlns:o-ex=\"http://odrl.net/1.1/ODRL-EX\"" + newline +

"\txmlns:o-ex=\"http://odrl.net/1.1/ODRL-DD\"" + newline + ">" + newline);

pw.println(str.toString());

fos.flush();

fos.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

filestreama at 2007-7-9 14:02:18 > top of Java-index,Java Essentials,Java Programming...