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 12 of 12

Thread: BufferedWriter help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question BufferedWriter help

    java how to write file in one line separate in nextline meaning write into two copies


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Buferred Writer in TextFile

    how to write text in notepad only first line except second line and others ?
    can you give me sample code?

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Buferred Writer in TextFile

    Post what code you have done so far and then we can assist you on any questions you may have about your code.

    For more information on class BufferedWriter: BufferedWriter (Java 2 Platform SE v1.4.2)

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: BufferedWriter help

    I assume this is the same thread as the one you made after this.

    Post what code you have done so far and then we can assist you on any questions you may have about your code.

    For more information on class BufferedWriter: BufferedWriter (Java 2 Platform SE v1.4.2)

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: BufferedWriter help

    Hi there. this my code i've done:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
     
    public class RegisterBank extends JFrame {
    		private String gender[] = {"male","female"};
    		private String bmonth[]={"January","February","March","April","May",
    		"June","July","August","September","October","November","December"};
    		private String year[]={"2013","2012","2011","2010","2009","2008","2007",
    		"2006","2005","2004","2003","2002","2001","2000","1999","1998","1997",
    		"1996","1995","1994","1993","1992","1991","1990","1989","1988","1987",
    		"1986","1985","1984","1983","1982","1981","1980","1979","1978","1977",
    		"1976","1975","1974","1973","1972","1971","1970","1969","1968","1967",
    		"1966","1965","1964","1963","1962","1961","1960"};
    		private String day[] = {"1","2","3","4","5","6","7","8","9","10",
    		"11","12","13","14","15","16","17","18","19","20","21","22","23","24",
    		"25","26","27","28","29","30","31"};
     
     
         public RegisterBank (){
     
    		JButton back = new JButton("Back");
    		back.setLocation(420,220);
    		back.setFocusable(false);
    		back.setBackground(Color.CYAN);
    		back.setForeground(Color.BLACK);
    		back.setFont(new Font("Tahoma", Font.BOLD, 12));
    		back.setSize(90,35);
    		back.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			dispose();
    			}
    		}
    		);
    		/*ImageIcon piggybank = new ImageIcon(getClass().getResource("/images/pig.gif"));
    		final JLabel pig = new JLabel(piggybank);*/
    		pig.setLocation(545,175);
    		pig.setForeground(Color.BLACK);
    		pig.setSize(90,90);
     
    		final JLabel rt = new JLabel("*****REGISTRATION-FORM*****");
    		rt.setFont(new Font("Batang", Font.BOLD, 32));
    		rt.setLocation(30,0);
    		rt.setForeground(Color.BLACK);
    		rt.setSize(650,50);
     
    		final JLabel acn = new JLabel("Account No. :");
    		acn.setFont(new Font("Serif", Font.BOLD, 16));
    		acn.setLocation(50,60);
    		acn.setForeground(Color.BLACK);
    		acn.setSize(200,30);
     
    		final JLabel st = new JLabel("Location :");
    		st.setFont(new Font("Serif", Font.BOLD, 16));
    		st.setLocation(325,60);
    		st.setForeground(Color.BLACK);
    		st.setSize(200,30);
     
    		final JLabel fn = new JLabel("First Name :");
    		fn.setFont(new Font("Serif", Font.BOLD, 16));
    		fn.setLocation(50,100);
    		fn.setForeground(Color.BLACK);
    		fn.setSize(200,30);
     
    		final JLabel ln = new JLabel("Last Name :");
    		ln.setFont(new Font("Serif", Font.BOLD, 16));
    		ln.setLocation(270,100);
    		ln.setForeground(Color.BLACK);
    		ln.setSize(150,30);
     
    		final JLabel mi = new JLabel("MI :");
    		mi.setFont(new Font("Serif", Font.BOLD, 16));
    		mi.setLocation(480,100);
    		mi.setForeground(Color.BLACK);
    		mi.setSize(150,30);
     
    		final JLabel em = new JLabel("E-mail :");
    		em.setFont(new Font("Serif", Font.BOLD, 16));
    		em.setLocation(325,140);
    		em.setForeground(Color.BLACK);
    		em.setSize(200,30);
     
    		final JLabel pnc = new JLabel("Pincode :");
    		pnc.setFont(new Font("Serif", Font.BOLD, 16));
    		pnc.setLocation(50,140);
    		pnc.setForeground(Color.BLACK);
    		pnc.setSize(200,30);
     
    		final JLabel gl = new JLabel("Gender :");
    		gl.setFont(new Font("Serif", Font.BOLD, 16));
    		gl.setLocation(50,180);
    		gl.setForeground(Color.BLACK);
    		gl.setSize(200,30);
     
    		final JLabel bd = new JLabel("Birth Date :");
    		bd.setFont(new Font("Serif", Font.BOLD, 16));
    		bd.setLocation(265,180);
    		bd.setForeground(Color.BLACK);
    		bd.setSize(200,30);
     
    		final JComboBox gen = new JComboBox (gender);
            gen.setMaximumRowCount(2);
            gen.setBackground(new Color(204,204,255));
            //gen.setFocusable(false);
            gen.setFont(new Font("Tahoma",Font.BOLD, 14));
            gen.setForeground(Color.BLACK);
            gen.setLocation(150, 180);
            gen.setSize(100, 25);
     
    		final JTextField accountnoTF = new JTextField(20);
    		accountnoTF.setSelectionColor(Color.MAGENTA);
    		accountnoTF.setLocation(150,60);
    		accountnoTF.setBackground(new Color(204,204,255));
    		accountnoTF.setFont(new Font("Serif", Font.PLAIN, 16));
    		accountnoTF.setToolTipText("Type your Account ID or Number in textbox.");
    		accountnoTF.setSize(150,25);
    		accountnoTF.setHorizontalAlignment(JTextField.LEFT);
     
    		final JTextField address = new JTextField(20);
    		address.setSelectionColor(Color.MAGENTA);
    		address.setLocation(410,60);
    		address.setBackground(new Color(204,204,255));
    		address.setFont(new Font("Serif", Font.PLAIN, 16));
    		address.setToolTipText("Type your Account Name in textbox.");
    		address.setSize(150,25);
    		address.setHorizontalAlignment(JTextField.LEFT);
     
    		final JTextField fnameTF = new JTextField(20);
    		fnameTF.setSelectionColor(Color.MAGENTA);
    		fnameTF.setLocation(150,100);
    		fnameTF.setBackground(new Color(204,204,255));
    		fnameTF.setFont(new Font("Serif", Font.PLAIN, 16));
    		fnameTF.setToolTipText("Type your First Name in textbox.");
    		fnameTF.setSize(100,25);
    		fnameTF.setHorizontalAlignment(JTextField.LEFT);
     
    		final JTextField lnameTF = new JTextField(20);
    		lnameTF.setSelectionColor(Color.MAGENTA);
    		lnameTF.setLocation(360,100);
    		lnameTF.setBackground(new Color(204,204,255));
    		lnameTF.setFont(new Font("Serif", Font.PLAIN, 16));
    		lnameTF.setToolTipText("Type your Last Name in textbox.");
    		lnameTF.setSize(100,25);
    		lnameTF.setHorizontalAlignment(JTextField.LEFT);
     
    		final JTextField miTF = new JTextField(2);
    		miTF.setSelectionColor(Color.MAGENTA);
    		miTF.setLocation(520,100);
    		miTF.setBackground(new Color(204,204,255));
    		miTF.setFont(new Font("Serif", Font.PLAIN, 16));
    		miTF.setToolTipText("Type your Middle Name in textbox.");
    		miTF.setSize(40,25);
    		miTF.setHorizontalAlignment(JTextField.CENTER);
     
    		final JTextField email = new JTextField(20);
    		email.setSelectionColor(Color.MAGENTA);
    		email.setLocation(410,140);
    		email.setBackground(new Color(204,204,255));
    		email.setFont(new Font("Serif", Font.PLAIN, 16));
    		email.setToolTipText("Type your E-mail in textbox.");
    		email.setSize(150,25);
    		email.setHorizontalAlignment(JTextField.LEFT);
     
    		final JPasswordField pincodePF = new JPasswordField(20);
    		pincodePF.setSelectionColor(Color.MAGENTA);
    		pincodePF.setLocation(150, 140);
    		pincodePF.setBackground(new Color(204,204,255));
    		pincodePF.setEchoChar('*');
    		pincodePF.setFont(new Font("Serif", Font.PLAIN, 16));
    		pincodePF.setToolTipText("Type 8-16 characters.");
    		pincodePF.setSize(150,25);
    		pincodePF.setHorizontalAlignment(JTextField.LEFT);
     
    		final JComboBox bd1 = new JComboBox (bmonth);
            bd1.setMaximumRowCount(4);
            bd1.setBackground(new Color(204,204,255));
            //bd1.setFocusable(false);
            bd1.setFont(new Font("Serif",Font.BOLD, 12));
            bd1.setForeground(Color.BLACK);
            bd1.setLocation(350, 180);
            bd1.setSize(85, 25);
     
            final JComboBox bd2 = new JComboBox (year);
            bd2.setMaximumRowCount(4);
            bd2.setBackground(new Color(204,204,255));
            //bd2.setFocusable(false);
            bd2.setFont(new Font("Serif",Font.BOLD, 12));
            bd2.setForeground(Color.BLACK);
            bd2.setLocation(485, 180);
            bd2.setSize(55, 25);
     
            final JComboBox bd3 = new JComboBox (day);
            bd3.setMaximumRowCount(4);
            bd3.setBackground(new Color(204,204,255));
            //bd3.setFocusable(false);
            bd3.setFont(new Font("Serif",Font.BOLD, 12));
            bd3.setForeground(Color.BLACK);
            bd3.setLocation(440, 180);
            bd3.setSize(40, 25);
     
    		final JButton clr = new JButton("Clear");
    		clr.setLocation(300,220);
    		clr.setFocusable(false);
    		clr.setBackground(Color.CYAN);
    		clr.setForeground(Color.BLACK);
    		clr.setFont(new Font("Tahoma", Font.BOLD, 12));
    		clr.setSize(90,35);
    		clr.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				accountnoTF.setText("");
    				address.setText("");
    				fnameTF.setText("");
    				lnameTF.setText("");
    				pincodePF.setText("");
    				email.setText("");
    				miTF.setText("");
    				gen.setSelectedIndex(0);
    				bd1.setSelectedIndex(0);
    				bd2.setSelectedIndex(0);
    				bd3.setSelectedIndex(0);
     
    			}
     
    		});
     
    		final JButton rgstr =new JButton("Register");
    		rgstr.setLocation(180,220);
    		rgstr.setFocusable(false);
    		rgstr.setBackground(Color.CYAN);
    		rgstr.setForeground(Color.BLACK);
    		rgstr.setFont(new Font("Tahoma", Font.BOLD, 12));
    		rgstr.setSize(90,35);
    		rgstr.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
     
    			if ((fnameTF.getText().equals(""))||
    				(lnameTF.getText().equals(""))||
    				(pincodePF.getText().equals(""))||
    				(miTF.getText().equals(""))||
    				(accountnoTF.getText().equals(""))||
    				(address.getText().equals(""))||
    				(email.getText().equals(""))){
    				JOptionPane.showMessageDialog(null,
    					"Please complete the details...",
    					"Incomplete",
    					JOptionPane.WARNING_MESSAGE);
    			}else{
    				JOptionPane.showMessageDialog(null,
    					"Thanks For Register",
    					"New Data",
    					JOptionPane.INFORMATION_MESSAGE);
    					LoginBank exe = new LoginBank();
    					dispose();
     
     
    				BufferedWriter writer = null;
    		    	try {
    		    		File file = new File("Database.txt");
    		          	String text="";
    		           	writer = new BufferedWriter(new FileWriter(file, true));
     
    		          	text = text + accountnoTF.getText() + "-" +
    		          		pincodePF.getText()+ "-" +
    		          		fnameTF.getText() + "-" +
    		          		lnameTF.getText() + "-" +
    		          		miTF.getText() + "-" +
    		          		bd1.getSelectedItem()+" "+
    		          		bd3.getSelectedItem()+", "+
    		          		bd2.getSelectedItem()+"-"+
    		          		gen.getSelectedItem()+"-"+
    		          		email.getText() + "-" +
    		          		address.getText()+ ":";		          		
    		          		writer.write(text);
     
     
    		     	} catch (FileNotFoundException ex){
    		          	ex.printStackTrace();
    		      	} catch (IOException ex) {
    		            ex.printStackTrace();
    		       	} finally {
    		            try {
    		               	if (writer != null){
    		                	writer.close();
    		              	}
    		              	System.out.println("The data has been written");
    		           	} catch (IOException ex) {
    		                ex.printStackTrace();
    		           	}
    		        }
     
    			}
    		}
    		});
     
     
     
     
    		Container pane = getContentPane();
    		pane.setLayout(null);
    		pane.add(back);
    		pane.add(rgstr);
    		pane.add(accountnoTF);
    		pane.add(address);
    		pane.add(fnameTF);
    		pane.add(lnameTF);
    		pane.add(pincodePF);
    		pane.add(acn);
    		pane.add(st);
    		pane.add(fn);
    		pane.add(ln);
    		pane.add(pnc);
    		pane.add(rt);
    		pane.add(clr);
    		pane.add(em);
    		pane.add(email);
    		pane.add(gen);
    		pane.add(gl);
    		pane.add(bd);
    		pane.add(miTF);
    		pane.add(mi);
    		pane.add(pig);
    		pane.add(bd1);
    		pane.add(bd2);
    		pane.add(bd3);
     
    		/*setIconImage(new ImageIcon("icon1.jpg").getImage());
    		setLayout(new BorderLayout());
    		JLabel reg = new JLabel(new ImageIcon(getClass().getResource("/images/reg.jpg")));
    		add(reg);*/
    		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		setTitle("Register Account");
    		setSize(650,300);
    		setResizable(false);
    		setVisible(true);
    		setLocation(180, 100);
         }
         public static void main(String[] args){
     
    		RegisterBank exe = new RegisterBank();
     
    	}
    }

    And there is the my text file i want to write like this..
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:0721-1234-Rhoan-Mariano-T.-February 1, 1995-male-@gmail.com-Taguig:2121-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig:2121-12321-wilon-ddasa-a-January 1, 1995-male-lordwil0-bataan:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    0721-1234-Rhoan-Mariano-T.-February 1, 1995-male-@gmail.com-Taguig
     
    2121-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig

    And there is my writer:
    BufferedWriter writer = null;
    		    	try {
    		    		File file = new File("Database.txt");
    		          	String text="";
    		           	writer = new BufferedWriter(new FileWriter(file, true));
     
    		          	text = text + accountnoTF.getText() + "-" +
    		          		pincodePF.getText()+ "-" +
    		          		fnameTF.getText() + "-" +
    		          		lnameTF.getText() + "-" +
    		          		miTF.getText() + "-" +
    		          		bd1.getSelectedItem()+" "+
    		          		bd3.getSelectedItem()+", "+
    		          		bd2.getSelectedItem()+"-"+
    		          		gen.getSelectedItem()+"-"+
    		          		email.getText() + "-" +
    		          		address.getText()+ ":";		          		
    		          		writer.write(text);
     
     
    		     	} catch (FileNotFoundException ex){
    		          	ex.printStackTrace();
    		      	} catch (IOException ex) {
    		            ex.printStackTrace();
    		       	} finally {
    		            try {
    		               	if (writer != null){
    		                	writer.close();
    		              	}
    		              	System.out.println("The data has been written");
    		           	} catch (IOException ex) {
    		                ex.printStackTrace();
    		           	}
    		        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BufferedWriter help

    Can you explain the problem you are having? What does the program do wrong?
    How do you want to change what the program does?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: BufferedWriter help

    my problem is how to write file using notepad where the firstline is looping in one line like this
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:0721-1234-Rhoan-Mariano-T.-February 1, 1995-male-@gmail.com-Taguig:2121-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig:2121-12321-wilon-ddasa-a-January 1, 1995-male-lordwil0-bataan:

    and other looping is write every nextline like this:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    0721-1234-Rhoan-Mariano-T.-February 1, 1995-male-@gmail.com-Taguig
    2121-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig

    so the output of textfile should be is :
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:0721-1234-Rhoan-Mariano-T.-February 1, 1995-male-@gmail.com-Taguig:2121-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig:2121-12321-wilon-ddasa-a-January 1, 1995-male-lordwil0-bataan:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    0721-1234-Rhoan-Mariano-T.-February 1, 1995-male-@gmail.com-Taguig
    2121-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig


    --- Update ---

    for example:
    When I Register or write a new account the output is:

    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city

    then, when I register or write again:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city
    21-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-taguig city

    just like that

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BufferedWriter help

    Can you explain what the code does wrong? What is the problem with it that you want to change?
    You posted several lines of text but don't say what the problem is and what you want to change. You have to tell us what is wrong with the program now and what you want it to do differently.

    The posted code takes data for one line and writes that one line at the end of the current text file.
    How do you want to change that?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Check This! :-?

    How to loop this writer:
    I would like to separate write in notepad or textfile.
    Example:

    Should the writer like this I want to loop is by first line or one line only.
    writer.write(text);

    Then,Should the writer like this I would like to loop by every nextLine.
    writer.write(textline);


    I want to seperate this! I need your help ..

    I hope there so many intelligent programmer on this site



  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Check This! :-?

    I'm not sure that I fully understand what you're asking. Can you try to clarify your question by providing both more detail and more pertinent code?

  11. #11
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Check This! :-?

    This is your fourth thread on the same issue. You should wait for someone to respond to your first thread instead of re-posting the same question.

    First Thread
    Second Thread
    Third Thread

    Also on the BufferedWriter class: BufferedWriter

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: BufferedWriter help

    I agree. Stick with one thread, else you're not being fair to the volunteers who help on this forum.

Similar Threads

  1. BufferedWriter Problem
    By dailywalker in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 4th, 2011, 10:44 PM