This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes How to insert new line a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to insert new line a text file" Watch "How to insert new line a text file" New topic

How to insert new line a text file

Dhiraj Srivastava
Ranch Hand

Joined: Aug 29, 2001
Posts: 49
I want to insert a new line in a text file which I generate from my code.
Following is the code snippet. Please see the output below. Why spaces are also coming between every character



public class LetterGeneration
public static void main(String[] args)
DataOutputStream fout = new DataOutputStream(new BufferedOutputStream (new FileOutputStream("Lines.Txt")));
fout.writeChars("This is first line");
fout.writeChars("This is second line");
catch (Exception e)
System.out.println("Exception occured..");


Output : Lines.Txt
T h i s i s f i r s t l i n e T h i s i s s e c o n d l i n e

I want like this..
This is first line
This is second line

Thanks in advance..
Joe Ess

Joined: Oct 29, 2001
Posts: 8836

DataOutputStream does not write plain text files. It writes binary files in a platform-independent manner. Have a look at the Java API documentation for the details.
What you want to use is some form of Writer. Have a look at the Java Tutorial chapter on IO for the options available and what each is used for.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Dhiraj Srivastava
Ranch Hand

Joined: Aug 29, 2001
Posts: 49

Thanks I got the solution.. Its easy to wrap the FileWriter Stream into the PrintWriter..
Following is the code..
FileWriter fw = new FileWriter(<FILE NAME> ;
PrintWriter pw = new PrintWriter(fw);




Thanks for help...

Success is not a 100 meter race its like a marathon where you have to show your persistence and courage little longer.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
If you pretent to write text into the file then you could simple invoke the newLine method in the BufferedWriter.
nm patel

Joined: Oct 03, 2005
Posts: 2
i think this will work for u...


public class LetterGeneration{
public static void main(String[] args){
FileWriter fwrite = new FileWriter("Lines.Txt");
fwrite.write("This is first line\n");
fwrite.write("This is second line");
}catch (Exception e){
System.out.println("Exception occured..");

Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Originally posted by nm patel:
fwrite.write("This is first line\n");

\n will create unix style line ending, but other system use different line endingd so is it better to use PrintWriter and println method.
It is sorta covered in the JavaRanch Style Guide.
subject: How to insert new line a text file
Similar Threads
Alphabet Pyramid (this one!)
I want to merge the two files into one of kind: by placing englishKeyword=English in format Russian
find and replace
How can I change the character in mid of the file
Tomcat madness!