Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to insert new line a text file  RSS feed

 
Dhiraj Srivastava
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhiraj Srivastava
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you pretent to write text into the file then you could simple invoke the newLine method in the BufferedWriter.
 
nm patel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!