aspose file tools*
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
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: 8968
    
    9

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.


[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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to insert new line a text file