Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

creating file in documents folder  RSS feed

 
Jason McKinley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried and tried to find out how to do this but can't find any information on it. What I am wanting to do is create a file in the documents folder but since I am going to be giving the program out to family and friends and their file path is going to be different than mine I don't know how to go about writing it.

I know you use

to make the file bit I just don't know how to get it to make it in the documents folder.

I would also like to make it possible to use on linux machines since I have a few people I want to give it to using linux.

Here is my code I want to implement the code into:

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using a JFileChooser.
when you call showSaveDialog(), it is automatically set for the Documents folder.
oh wait..my bad...you are trying to create it in code. not through the user
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the "documents" folder is. But anyway I'm with Randall, you should ask your users where they want the file put. Perhaps they use their "documents" folder for something else and don't want your application putting files there -- who knows?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a default path to start from, you could use the user.home System property (http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html), then drill down from there. For example, to put it in a path under 'My Documents' you could do


The above code is pseudocode, so will likely need fixing. It uses (in addition to System properties) java.nio.file.Paths and java.nio.file.Path, both part of Java 7.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how this can work across platforms. For e.g. on my Mac, there is no "My Documents" but "Documents". I do not think there is any universal property for (My) Documents like "user.home"
The only workaround I can think of is
Default to user.home
Let the user choose where to save.
Remember the last saved location (e.g. Using the preferences API)
Next time default to the last saved location.

 
Jason McKinley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:If you want a default path to start from, you could use the user.home System property (http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html), then drill down from there. For example, to put it in a path under 'My Documents' you could do


The above code is pseudocode, so will likely need fixing. It uses (in addition to System properties) java.nio.file.Paths and java.nio.file.Path, both part of Java 7.


I have changed where I will be saving the information to the appdata/roaming folder. I used your code to do that but now my problem is how do I change it in my code:



I can't figure out how to exactly implement the new code into this part.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!