Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to Overwrite

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please have a look at the following code.



In this code, when the user clicks the save button, a JFileChooser appears and saves the file in the selected location. But, the problem is, I need to over wright the file if it exists, and this code won't do that. And the miracle thing is, if I remove everything related to JFileChooser and gave a direct access to a file as the parameter in FileWriter, then it works!! Please help...
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have this line:



and from here: http://docs.oracle.com/javase/6/docs/api/java/io/File.html#createNewFile%28%29

you can read that it will return false if file already exists. AND it

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please show me my mistakes..


overwrite instead of overwright. Unable to overwrite.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help please?
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim McGuire wrote:
please show me my mistakes..


overwrite instead of overwright. Unable to overwrite.


Oh god!! That's why I didn't get any reply for a long time. Thanks a lot for pointing out the mistake
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I deleted that line, still no good
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, you don't want to delete that line. Otherwise no file will ever get written.

Instead, you have to handle it when that method call returns false.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the problem. Just delete this line

fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic