• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to insert new line a text file

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..");
}
}
}

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic