?
Solved

newline has no effect

Posted on 2003-02-19
5
Medium Priority
?
176 Views
Last Modified: 2010-03-31
I  have the following code to write a few lines to a text file. But new lines have no effect, every new line comes as a square symbol instead of forcing a newline:
import java.io.*;

class WriteTextFile2
{

public static void main ( String[] args )
{
String fileName = "reaper3.txt" ;

try
{

FileWriter writer = new FileWriter( fileName );

writer.write( "Alone she cuts and binds the grain,\n" );
writer.write( "And sings a melancholy strain;\n" );
writer.write("\n\n");
writer.write( "O listen! for the Vale profound\n" );
writer.write( "Is overflowing with the sound.\n\n" );

writer.close();
}
catch ( IOException iox )
{
System.out.println("Problem writing " + fileName );
}
}
}


Many thanks
akz
0
Comment
Question by:akz99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7983057
don't forget carriage return char:

writer.write( "Alone she cuts and binds the grain,\r\n" );
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 7983189
Using a PrintWriter will save a) you the trouble of adding line separators and b) ensure that the right line separator for your platform is used:

import java.io.*;

class WriteTextFile2
{

public static void main ( String[] args )
{
String fileName = "reaper3.txt" ;

try
{

FileWriter writer = new FileWriter( fileName );
PrintWriter out = new PrintWriter(writer, true);

out.println( "Alone she cuts and binds the grain,");
out.println( "And sings a melancholy strain;" );
out.println();
out.println( "O listen! for the Vale profound" );
out.println( "Is overflowing with the sound." );

out.close();
}
catch ( IOException iox )
{
System.out.println("Problem writing " + fileName );
}
}
}



0
 

Expert Comment

by:CleanupPing
ID: 9059228
akz99:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9553077
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
ANSWERED by: CEHJ

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question