Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

newline has no effect

Posted on 2003-02-19
5
Medium Priority
?
177 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
4 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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

578 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