Volatil3
Volatil3

Reputation: 14988

Can Java Preference API be used as an alternaitve to avoid reading File?

In my program there's requirement to read file content at the time of program startup. Right now file is loaded in the constructor. Since file is a bit huge and I/O is being performed so it takes a bit of time to show screen.

My question is, can I use Preference API as an aternative of file I/O? Since content is not changed frequently so I want to avoid I/O unless it's asked by user. I would like to load content once if preference not set and as long as preference is not empty it fetches content from Preference rather than file.

Share your thoughts.

Upvotes: 1

Views: 59

Answers (2)

trashgod
trashgod

Reputation: 205805

@Zhedar's point about Preferences is well taken. Instead, load the file in the background using SwingWorker, as shown here. With suitable granularity in publish() and process(), results can begin appearing immediately. The GUI will remain responsive while the remainder of the file loads.

Upvotes: 1

Zhedar
Zhedar

Reputation: 3510

I would say it wouldn't make a difference if performance matters.
A Preference is nothing but a file on your disk, too. In fact on UNIX and Mac OS X-Computers it would be saved in an XML-File, Windows puts Preferences in the registry. You may look in the wrong direction here.
Is your file that big? Then don't load it in the main thread.
To achieve that, do long running IO-Operations in a different thread and show a loading screen or something like that instead. Since you don't said which UI technology you use I cannot provide any specific information to achieve that. If you're using swing I recommend you to take a look into the SwingWorker class.

Upvotes: 1

Related Questions