Why the file.txt is not updated inside the IDE (using BufferedWriter)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redcodeheaven
    New Member
    • Nov 2008
    • 13

    #1

    Why the file.txt is not updated inside the IDE (using BufferedWriter)?

    Hi all ,I came across the internet for such a method to write to a file,although I am taking java but never really learned about buffered writer instead I took Printstream class;my question is why the records of the file created using this method are not updated (if Ineed to add records) inside IntellijIdea,in stead you have always to check the file using Notepad,thanks for any reply here is the code:

    Code:
    public void writeToFile(String filename){
    			
    
    			BufferedWriter bufferedWriter = null;
    
    			try {
    
    				// Construct the BufferedWriter object
    				bufferedWriter = new BufferedWriter(new FileWriter(filename));
    
    				// Start writing to the output stream
    				bufferedWriter.write("Writing line one to file");
    				bufferedWriter.newLine();
    				bufferedWriter.write("Writing line two to file");
    				bufferedWriter.newLine();                        
    				bufferedWriter.write("Writing line three to file");
    			} catch (FileNotFoundException ex) {
    				ex.printStackTrace();
    			} catch (IOException ex) {
    				ex.printStackTrace();
    			} finally {
    
    				// Close the BufferedWriter
    				try {
    					if (bufferedWriter != null){
    						bufferedWriter.flush();
    						bufferedWriter.close();
    					}
    				} catch (IOException ex) {
    					ex.printStackTrace();
    				}
    			}
    		}
    Last edited by JosAH; Apr 9 '09, 06:20 AM. Reason: added [code] ... [/code] tags
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Please use "code tag". By the way i tested this code, it's running successfully. But what do you mean by "inside the IDE"?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Each time you run that code it'll create a new file (the old contents are lost). If that's not what you want read the API documentation for the FileWriter class; it also has a constructor that appends your data to the already existing contents.

      kind regards,

      Jos

      Comment

      Working...