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

Thread: probleming writing multiple items to text file

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default probleming writing multiple items to text file

    Hello all,
    I am taking a graduate seminar that involves Java. I am working on an assignment where I create an interface that will capture a books title, author, and availability. The end result is to write this to a file 5 times. However, I am finding that each time I press the "save" button on my interface, it writes to the text file but replaces the text that was there before.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
     
    public class TryThis extends JFrame {
     
    	private JPanel panel;
    	private JButton button;
    	private JCheckBox check;
    	private JTextField bookTitle;
    	private JTextField authorField;
    	private JLabel bookLabel;
    	private JLabel authorLabel;
    	String title;
    	String author;
    	String available;
     
    	public TryThis(String str) {
    		super(str);
     
    	}
     
    	public static void main(String[] args) {
     
    		TryThis myGUI = new TryThis("Save book records");
    		myGUI.createAndShowGUI();
    		// myGUI.writeTofile();
    	}
     
    	private void createAndShowGUI() {
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new BorderLayout());
    		contentPane.setLayout(null);
     
    		JButton button = new JButton("Save");
    		button.setBounds(230, 125, 75, 20);
    		this.add(button);
    		button.addActionListener(new BookEntry());
     
    		check = new JCheckBox("Available", false);
    		check.setBounds(230, 100, 100, 20);
    		this.add(check);
     
    		bookLabel = new JLabel("Book title:");
    		bookLabel.setBounds(10, 10, 100, 20);
    		this.add(bookLabel);
    		bookTitle = new JTextField();
    		bookTitle.setBounds(120, 10, 350, 20);
    		this.add(bookTitle);
     
    		authorLabel = new JLabel("Author:");
    		authorLabel.setBounds(10, 50, 100, 20);
    		this.add(authorLabel);
    		authorField = new JTextField();
    		authorField.setBounds(120, 50, 350, 20);
    		this.add(authorField);
     
    		setSize(500, 200);
     
    		setVisible(true);
     
    	}
     
    	public class BookEntry implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    			title = bookTitle.getText();
    			author = authorField.getText();
    			System.out.println(title);
    			System.out.println(author);
    			if (check.isSelected()) {
    				available = "available";
     
    			} else {
    				available = "not available";
     
    			}
    			System.out.println(available);
     
    			StringBuilder result = new StringBuilder();
    			result.append(title);
    			result.append(author);
    			result.append(available);
     
    			try { // all the I/O stuff must be a in try/catch
    				File file = new File("book.txt");
    				if (!file.exists()) {
    					file.createNewFile();
    					System.out.println("new file:" + file);
    				}
     
    				BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    				writer.write(result.toString());
    				writer.close();
     
    			} catch (Exception ex) {
    				ex.printStackTrace();
    			}
    		}
    	}
     
    }

    What am I doing wrong?

    Thank you.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: probleming writing multiple items to text file

    See the API for FileWriter
    FileWriter (Java Platform SE 6)
    There is a constructor that takes a boolean option which allows you to append to a given file, otherwise it will by default overwrite the file.

Similar Threads

  1. Help writing to a text file on a website!
    By straw in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 11:02 AM
  2. Create Multiple Arrays from a text file
    By chris11kgf in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2011, 12:40 AM
  3. 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, 08:11 PM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 06:16 PM