Hello

I am using windows vista and the problem is that \r i.e., carriage return does not work properly on my system. When I run my java code, it inserts a new line rather than returning the carriage.

Can anyone suggest a solution to this problem?

The system will generate, what the system will generate.
It's NOT that it does not work properly on YOUR system,
It's just NOT what you are expecting.
You will have to handle (process) what the system(s) will generate.

This seems like a display issue. Where exactly are you observing this behaviour? If in an IDE console, can you try running the same code from command line and check whether it works or not?

carriage return does not work properly

Can you describe the proper working of a carriage return.
What happens depends on what software is reading and outputing the text from the file.
With some printers, the carriage return will move the print position to the beginning of the current line, not advancing a line.

I am not observing the output in a display. I am dumping the output in a file and when I open it, I find that the behavior of /r is like /n.

Can you describe the proper working of a carriage return.

What is the "display" where you see the output.
What program do you open the file with?

What happens depends on what software is reading and outputing the text from the file.
Does the documentation for the software you are using describe what it will do with the \r and\or \n values?

Edited 5 Years Ago by NormR1: n/a

I am not observing the output in a display. I am dumping the output in a file and when I open it, I find that the behavior of /r is like /n.

On some platforms (namely Mac), `\r` is a newline character. It is quite possible that the editor you are opening the file in is "inferring" the end of line character and displaying it as a "newline" character. Which editor are you opening the file in?

Proper working of carriage return = To take the cursor to the start of the line

I am working using the command prompt cmd of windows vista

Carriage Return (CR, ASCII 13) is "Old School" (with a teletype output).
The CR was to send it back to the beginning of the line WITHOUT a Line Feed (LF, ASCII 10).
Systems, back then, required both the CR and LF.
Most systems today do not operate in this manner.
You should only expect the LF.
If you have a file that contains the CR, you should be able to ignore it.

It is working perfectly fine when I use:

System.out.print

but when I use

outputStream.print

The function of \r starts behaving like \n.

It is working perfectly fine when I use:

System.out.print

but when I use

outputStream.print

The function of \r starts behaving like \n.

You have got to understand that "carriage return" is a control character and the behaviour of "returning the carriage" only makes sense for some devices. From the wikipedia entry:

In computing, the carriage return (CR) is one of the control characters in ASCII code, Unicode, EBCDIC, or many other codes. It commands a printer or other sort of output system such as a display to move the position of the cursor to the first position on the same line.

In a text editor, it makes little sense to again "move" to the beginning of the line and hence almost all text editors display it as a new line. Try this program:

public class Test {

	public static void main(String[] args) throws Exception {
		final File f = new File("c:/a.txt");
		final String cr = "\r";

		final PrintWriter pw = new PrintWriter(f);
		pw.print("Hi there, how are you?");
		pw.print(cr);
		pw.print("I'm fine good sir\nThank you");
		pw.close();
		
		final InputStream in = new FileInputStream(f);
		int i = -1;
		while ((i = in.read()) != -1) {
			System.out.print((char)i);
		}
	}

}

View the output file generated by the program in a text editor and compare it with the output printed on the console. What do you see?

Hello again,

\r is working perfectly fine when I use: System.out.print

\r behaves like \n when I use: outputStream.print

Any alternatives for this? The issue is that I have to send my output to a file and not just display it.

Thanks for any help!

Is there a problem if you use the System.getProperty("line.separator") String to end the lines in your file?

Comments
right +1

My output as displayed in the console is:

I'm fine good sir you?
Thank you


And the output shown using a text editor (i.e., for the output file a.txt) is:

Hi there, how are you?
I'm fine good sir
Thank you

The output is in accordance with what you said : "In a text editor, it makes little sense to again "move" to the beginning of the line and hence almost all text editors display it as a new line."


Would the usage of something other than PrintWriter work?

Edited 5 Years Ago by simply2smart: n/a

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

if returns null then is possible to define that System.setProperty("Whatever"), sure valid until current instance exists

> Would the usage of something other than PrintWriter work?

No, because all writers/streams would end up writing the CR character without giving consideration as to how it is displayed. You can only rely on the way text editors end up displaying the CR character. Or the simplest solution would be to "gobble" or "ignore" the previous characters in case you encounter a CR character. This way, the characters before CR won't be written to the text file and users of your text file won't be able to see it. But this again means that the contents of your text file would be different from the actual data (since you end up ignoring a line).

Edited 5 Years Ago by ~s.o.s~: n/a

Hello again,

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

It does return null. How should I set the property now?
Could you guide me a little bit more about it? Thanks everyone for the quick reponses

Hello again,

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

It does return null. How should I set the property now?
Could you guide me a little bit more about it? Thanks everyone for the quick reponses

The System.getProperty("line.separator") should return what you need. Why do you say it returns null. How did you test it?

If it still doesn't work you can try:

pw.println("Hi there, how are you?");
pw.println("I'm fine good sir");
pw.print("Thank you");

pw.close();

I haven't tested it, but even if it work you should still try to make the line.separator property work

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

I simply printed the above string alpha.

System.out.print(alpha + "");

and the output was:

null

Dear JavaAddict

I don't know how your code will help in solving the issue, could you explain it in more detail?

I haven't tested it but instead of writing:

pw.print("Hi there, how are you?");
pw.print("\n");
pw.print("I'm fine good sir");
pw.print("\n");

You could try:

pw.println("Hi there, how are you?");
pw.println("I'm fine good sir");

println VS print

Try these two lines and see what prints out:

Properties props = System.getProperties();
		props.list(System.out);

Also try using the java command that is in the jre vs the jdk version.
I see from the path you posted you are using the jdk version. My path shows I am using the jre version.

A little forray thru the Java API source code reveals this:
PrintStream's println method:

public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

new2Line() does some flushing etc, but calls the underlying BufferedWriter's newLine() method for the new line character(s).
BufferedWriter's newLine() method looks like this:

* Writes a line separator.  The line separator string is defined by the
     * system property <tt>line.separator</tt>, and is not necessarily a single
     * newline ('\n') character.
     *
     * @exception  IOException  If an I/O error occurs
     */
    public void newLine() throws IOException {
        write(lineSeparator);
    }

and finally here's the initialisation of lineSeparator

lineSeparator = java.security.AccessController.doPrivileged(
            new sun.security.action.GetPropertyAction("line.separator"));

So all println does over print is to add the "line.separator" property (and flushes the buffers). All that's different here is that it uses a privileged way to get that property. Interesting...

Nothing worked for me :(

I have decided to rewrite the code. Thanks anyways!

Use the BufferedWriter class.
It has a method:
newLine()
That adds a new line to the file that you are writing:

FileWriter fw = new FileWriter("file");
BufferedWriter bw = new BufferedWriter(fw);

bw.write("some string");
bw.newLine();

...

bw.close();

Edited 5 Years Ago by javaAddict: n/a

This article has been dead for over six months. Start a new discussion instead.