This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara 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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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
Author

How to insert new line a text file

Dhiraj Srivastava
Ranch Hand

Joined: Aug 29, 2001
Posts: 49
Hi,
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

/*****/

import java.io.*;

public class LetterGeneration
{
public static void main(String[] args)
{
try
{
DataOutputStream fout = new DataOutputStream(new BufferedOutputStream (new FileOutputStream("Lines.Txt")));
fout.writeChars("This is first line");
fout.writeChars("This is second line");
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
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..
Dhiraj
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

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
Hi,

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);

pw.println();

-------------------------

Anyways..

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
Greenhorn

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

import java.io.*;

public class LetterGeneration{
public static void main(String[] args){
try{
FileWriter fwrite = new FileWriter("Lines.Txt");
fwrite.write("This is first line\n");
fwrite.write("This is second line");
fwrite.close();
}catch (Exception e){
e.printStackTrace();
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.
 
Don't get me started about those stupid light bulbs.
 
subject: How to insert new line a text file