• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFileChooser saving a File to a certain directory

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have been searching for a solution to this problem for a few hours and now and would like to hear some ideas other people have had with this.



This is the code I am currently using to write to my desktop it does work as well.. but its not very good for other users. How would I accomplish the same task universally for whatever user whom decides to use this application for Mac & Windows?




 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I just accidentally stumbled upon my answer....

if I'm wrong please correct me, although I believe windows uses \ instead of MAC OSX / ? so there would need to be a if/else block to decide upon the OS





OUTPUT: /Users/john/Desktop/myCoolFile.txt
this code would equate to a file being written to users desktop in mac as I have just tested it
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't exactly said what you want to happen. It sort of looks like you want to save the file on the user's desktop, which might not even be a meaningful thing in some environments where Java runs. But I suppose you could choose some environments to support, and then do some hard-coding for each of those environments. But I'd really review whether you want to force the user to have that file on their desktop in the first place. Sure, it might work for you but other users might want it somewhere else. I know, it's always difficult trying to guess what somebody else might want.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,

Yes; it was just to save to a location in this case being the Desktop of the user with this project being a desktop application itself . I guess I could use the JFileChooser for this as well to let the user choose where they would like to put the file.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

travis Haycock wrote:

OUTPUT: /Users/john/Desktop/myCoolFile.txt


Instead of appending the user.name property to /Users/, try using the user.home property directly.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks Rob, that shortens it up a bit!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic