This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes file path retention Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "file path retention" Watch "file path retention" New topic
Author

file path retention

Philip Mat
Greenhorn

Joined: Jun 10, 2010
Posts: 10
I'm creating a monthly planner program in java swing. I'm planning to use a file to serialize the objects. Right now, I've hard coded the file path in the program.
What I want to do later is to ask the user to provide a file path and use that file path for serialization.
But I don't know how to retain or embed that file path in the program. When the user exits the program, the file path will be lost and next time when the program starts, it doesn't know where to find the old serialized file.
Can you help?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

you could write it to a configuration file. The program would start up, read the .cfg file and see if the path was defined or not.


Never ascribe to malice that which can be adequately explained by stupidity.
Philip Mat
Greenhorn

Joined: Jun 10, 2010
Posts: 10
But still I need to hard code the file path for the configuration file in the program, right?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 490

No, you can just take the user's input and write it to the configuration file, like Fred said. If they want to change the path allow the option to overwrite the existing path in the .cfg file.


Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

Philip Mat wrote:But still I need to hard code the file path for the configuration file in the program, right?
yes. Your program will need to know where the .cfg file is. You can hard-code it, you can have your install set a system parameter, you can ask the user each and every time...

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

Hunter McMillen wrote: No, you can just take the user's input and write it to the configuration file, like Fred said. If they want to change the path allow the option to overwrite the existing path in the .cfg file.

Hunter, read the question a little more closely. The OP is now asking if the CONFIG file needs to be hard-coded - not the file name for the serialization.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 490

You are correct, my mistake.

Hunter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Base it on System.getProperty("user.home"). Or use java.util.prefs.Preferences.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Philip Mat
Greenhorn

Joined: Jun 10, 2010
Posts: 10
Thanks all for your replies.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: file path retention
 
Similar Threads
jre path
Spring Config file
Installing java and putting it in the Path variable.
Creating a PATH?
Browsing for file Path