Results 1 to 3 of 3
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Help with printwriter.

    Hi!
    Im using print writter to write txt to a txt file.
    Now I have a problem.
    Yes when i start a new pritnwriter it will over write all other text
    So THERFORE I made it so that it will first write the previus content then
    write the new content.
    But STILL it over writes the previus content that it just added and replace it with the additional text!
    I have no clue why it does this!
    As you see here:

    Java Code:
        		try {
        		Scanner text2 = new Scanner(new FileReader(Path));
        		PrintWriter txtFile = new PrintWriter(new File(Path));
        		
        			while (text2.hasNext()){
    		    		String line = text2.next();
    		    		System.out.println(line);
    		    		txtFile.println(line);
        			}
        			txtFile.println(NameField.getText());
    		    	txtFile.println(AmountField.getText());
    		    	String s = (tixOrRobux.getSelectedItem()).toString();
    		    	txtFile.println(s);
        			txtFile.close();
        			new GameFrame();
        			this.dispose();
    	    	} catch (Exception e){
    	    		e.printStackTrace();
        		}
    It should write the previous content then write the new
    But all that is left is the new content!
    Why?

    Heres the whole script:
    PHP Code:
    import javax.swing.*; 
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.io.*;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import java.util.Vector;
    
    public class CNDF{
        
        public static void main(String[] args) throws IOException{
        	
        	GameFrame window = new GameFrame();
        	
        	//window. 
        }
    }
    
    class GameFrame extends JFrame implements ActionListener {
    	Vector<JButton> adds = new Vector<JButton>();
    	String Path = "C:\\Users\\Andreas\\Desktop\\CNDF.txt";
        JTextField NameField = new JTextField(30);	
        JTextField AmountField = new JTextField(30);    	
    	JComboBox tixOrRobux = new JComboBox();    	
        	
        public GameFrame() throws IOException {
        	Scanner text = new Scanner(new FileReader(Path));
        	int row = 0;
        	int amountRows = 0;
        	while (text.hasNext()){
        		String line = text.next();
        		System.out.println(line);
        		if (row == 0){
       				JLabel Name = new JLabel("    "+line+"     "); 
       				add(Name);			
        		}
        		if (row==1){
        			JLabel Amount =  new JLabel("|    "+line);
        			add(Amount);
        		}
        		if (row == 2){
        			JLabel Amount =  new JLabel("|     "+line);
        			add(Amount);
        			JButton info = new JButton("Info");
        			adds.add(info);
        			info.addActionListener(this);
        			add(info);	
        			amountRows++;
        		}
    			++row;
    			if (row == 3){
    				row = 0;
    			}	
        	}
        	text.close();
    
        	add(NameField);
    		tixOrRobux.addItem("Tix");
        	tixOrRobux.addItem("Robux");
        	add(AmountField);
        	add(tixOrRobux);
        	JButton Add = new JButton("Add");
        	Add.addActionListener(this);
        	add(Add);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		System.out.print(amountRows);
        	setLayout(new GridLayout(++amountRows,4));
        	//go.addActionListener(this);
        	pack();
        	setVisible(true);
        }
        
        public void actionPerformed(ActionEvent evt){
        	System.out.println("Clicked");
        	boolean clicked = false;
        	for (int x=0;x<adds.size();x++){
        		if (adds.get(x) == evt.getSource()){
        			System.out.println("you clicked button: "+Integer.toString(x));
        			clicked = true;
        		}
        	}
        	if (clicked == false){
        		try {
        		Scanner text2 = new Scanner(new FileReader(Path));
        		PrintWriter txtFile = new PrintWriter(new File(Path));
        		
        			while (text2.hasNext()){
    		    		String line = text2.next();
    		    		System.out.println(line);
    		    		txtFile.println(line);
        			}
        			txtFile.println(NameField.getText());
    		    	txtFile.println(AmountField.getText());
    		    	String s = (tixOrRobux.getSelectedItem()).toString();
    		    	txtFile.println(s);
        			txtFile.close();
        			new GameFrame();
        			this.dispose();
    	    	} catch (Exception e){
    	    		e.printStackTrace();
        		}
        	}
        }
    }
    Last edited by Addez; 10-30-2009 at 01:47 PM. Reason: WTH? I havent even typed to finish!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Use the PrintWriter with a FileWriter. The FileWriter class has a constructor that takes a boolean indicating whether you want to append or not.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    A PrintWriter can wrap another Writer; make that other Writer a FileWriter that appends to the file it has to write to; read the API documentation for those classes for details.

    kind regards,

    Jos

    edit: darn, too late again; I'm an old sod ;-)

Similar Threads

  1. Unexpected characters using PrintWriter
    By mjwoodford in forum New To Java
    Replies: 1
    Last Post: 09-18-2009, 06:09 PM
  2. difference printwriter & out.println
    By tushar88 in forum Java Servlet
    Replies: 1
    Last Post: 03-10-2009, 05:15 AM
  3. Using Printwriter (java.io.*)
    By LifeWithJava in forum New To Java
    Replies: 3
    Last Post: 12-25-2008, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •