Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: new line in file writing

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default new line in file writing

    package javaapplication25;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Emails extends WindowAdapter implements ActionListener
    {
    Frame f;
    TextField t1,t2;
    Button b; Label l1,l2;
    String s1,s2;
    String s3="f";
    String s4="hello@gmail.com";
    String s5,s6;
    String s7="00";
    String s8=",";
    FileOutputStream fos;
    File f1;
    String path,name;
    int count=0;
    public Emails()
    {

    f=new Frame();
    f.setSize(400,400);
    f.setLayout(new GridBagLayout());
    b=new Button("Create");
    b.addActionListener(this);
    l1=new Label("Year");
    l2=new Label("No Of Emails");
    t1=new TextField();
    t2=new TextField();
    GridBagConstraints gbc=new GridBagConstraints();
    gbc.gridx=0;gbc.gridy=0;
    gbc.gridwidth=2;gbc.gridheight=2;
    gbc.ipadx=4;gbc.ipady=6;
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbc.anchor=GridBagConstraints.NORTH;
    gbc.weightx=1.0;gbc.weighty=1.0;
    Insets i=new Insets(30,5,7,9);
    f.add(l1,gbc);
    gbc.gridx=2;
    f.add(t1,gbc);
    gbc.insets=i;
    gbc.gridy=2;
    gbc.gridx=0;
    f.add(l2,gbc);
    gbc.gridx=2;
    f.add(t2,gbc);
    Insets i2=new Insets(30,5,7,9);
    gbc.insets=i2;
    gbc.gridy=5;
    gbc.gridx=2;
    f.add(b,gbc);
    f.addWindowListener(this);
    f.setVisible(true);



    }

    public void actionPerformed(ActionEvent e)
    {
    try

    {
    String str=e.getActionCommand();
    if(str.equals("Create"))
    {

    BufferedWriter bw=null;
    path="D:/";
    name="email.txt";
    s1=t1.getText();
    s2=t2.getText();
    int a=Integer.parseInt(s2);


    f1=new File(path,name);
    if(f1.exists())
    f1.delete();
    fos = new FileOutputStream(f1);
    for(int i=0;i<a;i++)
    {
    count= count+1;
    /*if(count>9)
    {
    String c1=count+"";
    String s9="0";
    s5=s3+s1+s9+c1+s4+s8;
    byte c[] = s5.getBytes();
    fos.write(c);
    }*/
    s6=count+"";

    s5=s3+s1+s7+s6+s4+s8;
    byte c[] = s5.getBytes();
    fos.write(c);


    }

    }
    }
    catch(IOException ef)
    {

    }
    }

    public void windowClosing(WindowEvent e2)
    {
    Window w=e2.getWindow();
    w.setVisible(false);
    w.dispose();
    System.exit(1);
    }


    public static void main(String[] args)
    {
    Emails e=new Emails();
    }

    }

    this code will out put the text in a text file in one continuous line .. but i want to do that,, it will produce the output one line at a time ..how to do that plz help..

    like this
    f2011001hello@gmail.com
    f2011002hello@gmail.com

    not like

    f2011001hello@gmail.com,f2011001hello@gmail.com


  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: new line in file writing

    package javaapplication25;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Emails extends WindowAdapter implements ActionListener
    {
    Frame f;
    TextField t1,t2;
    Button b; Label l1,l2;
    String s1,s2;
    String s3="f";
    String s4="hello@gmail.com";
    String s5,s6;
    String s7="00";
    String s8=",";
    FileOutputStream fos;
    File f1;
    String path,name;
    int count=0;
    public Emails()
    {
     
    f=new Frame();
    f.setSize(400,400);
    f.setLayout(new GridBagLayout());
    b=new Button("Create");
    b.addActionListener(this);
    l1=new Label("Year");
    l2=new Label("No Of Emails");
    t1=new TextField();
    t2=new TextField();
    GridBagConstraints gbc=new GridBagConstraints();
    gbc.gridx=0;gbc.gridy=0;
    gbc.gridwidth=2;gbc.gridheight=2;
    gbc.ipadx=4;gbc.ipady=6;
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbc.anchor=GridBagConstraints.NORTH;
    gbc.weightx=1.0;gbc.weighty=1.0;
    Insets i=new Insets(30,5,7,9);
    f.add(l1,gbc);
    gbc.gridx=2;
    f.add(t1,gbc);
    gbc.insets=i;
    gbc.gridy=2;
    gbc.gridx=0;
    f.add(l2,gbc);
    gbc.gridx=2;
    f.add(t2,gbc);
    Insets i2=new Insets(30,5,7,9);
    gbc.insets=i2;
    gbc.gridy=5;
    gbc.gridx=2;
    f.add(b,gbc);
    f.addWindowListener(this);
    f.setVisible(true);
     
     
     
    }
     
    public void actionPerformed(ActionEvent e)
    {
    try
     
    {
    String str=e.getActionCommand();
    if(str.equals("Create"))
    {
     
    BufferedWriter bw=null;
    path="D:/";
    name="email.txt";
    s1=t1.getText();
    s2=t2.getText();
    int a=Integer.parseInt(s2);
     
     
    f1=new File(path,name);
    if(f1.exists())
    f1.delete();
    fos = new FileOutputStream(f1);
    for(int i=0;i<a;i++)
    {
    count= count+1;
    /*if(count>9)
    {
    String c1=count+"";
    String s9="0";
    s5=s3+s1+s9+c1+s4+s8;
    byte c[] = s5.getBytes();
    fos.write(c);
    }*/
    s6=count+"";
     
    s5=s3+s1+s7+s6+s4+s8;
    byte c[] = s5.getBytes();
    fos.write(c);
     
     
    }
     
    }
    }
    catch(IOException ef)
    {
     
    }
    }
     
    public void windowClosing(WindowEvent e2)
    {
    Window w=e2.getWindow();
    w.setVisible(false);
    w.dispose();
    System.exit(1);
    }
     
     
    public static void main(String[] args)
    {
    Emails e=new Emails();
    }
     
    }


    this code will out put the text in a text file in one continuous line .. but i want to do that,, it will produce the output one line at a time ..how to do that plz help..

    like this
    f2011001hello@gmail.com
    f2011002hello@gmail.com

    not like

    f2011001hello@gmail.com,f2011001hello@gmail.com

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: new line in file writing

    Add on a newline character ("\n") where you want the end of the line to be.

  4. The Following User Says Thank You to Norm For This Useful Post:

    deependeroracle (January 31st, 2012)

  5. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: new line in file writing

    Well, although Norm's solution will usually work, it is generally better to use a static variable retrieved from System.getProperty('line.separator"), to be technically more robust.

    An example of creating the String
    This is the first line and
    this is the second line
    could be done in one method like

    {
      String newLine = System.getProperty("line.separator");
      String str = "This is the first line and" + newLine + "this is the second line.";
    }

    Or for a class (A little faster)
    public class Example
    {
      private static final String newLine = System.getProperty("line.separator");
     
      public String exMethod()
      {
         return "This is the first line and" + newLine + "this is the second line.";
      }
    }

  6. The Following 2 Users Say Thank You to Tjstretch For This Useful Post:

    deependeroracle (January 31st, 2012), Norm (January 29th, 2012)

  7. #5
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: new line in file writing

    thnx buddy got that

Similar Threads

  1. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 09:11 PM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM