• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Save is not following the directory given to it by the SaveDialog  RSS feed

 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to have Java save a file in a directory of the user's choosing from the save file dialog. When it saves it however it is saving it in the default directory with the file name but not in the directory that the user chose. Below is the code. What more do I need to do to have the file saved where the user chooses?

Thank you for your help.


 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.getName() just returns the file name. Use the whole File object if you can, or use getPath() to get the whole path.
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, Thank you. That worked.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to check the return value of showSaveDialog. If the user presses Cancel getSelectedFile() will return null. showSaveDialog returns JFileChooser.APPROVE_OPTION if the user pressed OK, so if the return value is this value then continue, otherwise do something else (usually nothing).
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, Thanks for your post. showSaveDialog is returning a "1" (int) if the user chooses cancel and a "0" if they choose open. I am now checking for these values and only save if showSaveDialog returns a 0.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use "0" and "1" - use JFileChooser.APPROVE_OPTION and JFileChooser.CANCEL_OPTION instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!