Results 1 to 2 of 2
  1. #1
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default File overwriting?

    Basically I am trying to save to a csv file however it keeps overwriting my previous file, while instead I would like it to add to the end of the file. Here is my code :
    Java Code:
    	String generateCSVFile = "test.csv";
    		File f;
    		f = new File("test.csv");
    			    if(!f.exists()){
    		    	  try {
    					f.createNewFile();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    		    	  System.out.println("New file \"test.csv\" has been created to the current directory");
    		    	  }
    		
    		
    			  
    			try
    			{
    			    FileWriter writer = new FileWriter(generateCSVFile);
    			    BufferedWriter out = new BufferedWriter(writer);
    		 
    			    out.append(UsernameTxt.getText());
    			    out.append(',');
    			    out.append(PasswordFld.getText());
    			    out.append('\n');
    		 		 
    			    out.flush();
    			    out.close();
    			    NLbl.setText("File saved");
    			}
    			catch(IOException e)
    			{
    			     e.printStackTrace();
    			} 
    		    }
    Can you see what is wrong with it?

  2. #2
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: File overwriting?

    Figured out that I did not put the boolean for the end of file in the FileWriter part.

    Java Code:
     FileWriter writer = new FileWriter(generateCSVFile, true);

Similar Threads

  1. Writing data to file, but not overwriting it.
    By Renxx in forum New To Java
    Replies: 3
    Last Post: 12-10-2011, 11:14 PM
  2. Replies: 8
    Last Post: 07-05-2011, 07:32 PM
  3. Array overwriting data
    By hobo in forum New To Java
    Replies: 7
    Last Post: 10-27-2010, 02:29 PM
  4. Replies: 0
    Last Post: 03-05-2010, 08:11 PM
  5. Replies: 0
    Last Post: 02-11-2009, 10:53 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
  •