Creating a file that does not exist........

OK, here's a humdinger of a question.

I'm creating hashtables that are inputted into files of type .ser. Before putting a new entry into the hastable I read from a file that should contain the hashtable. However, if the file does not exist I want to be able to create the file to a specified name and then insert the hashtable entry. As of present it works if the file exists, otherwise it just bombs out and says FileNotFoundException. So, I reiterate, how can I create a file in my code?

Thank you
JavaJedi012400Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JodCommented:
You can create a File object to represent your file and then you can check whether it exists using the exists() method.

Then create it if neccessary using the createNewFile method.

EG:

public boolean createNewFile()
                      throws IOException

Automically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JodCommented:
In other words:

File f = new File("...myfile.txt");
if (!f.exists) f.createNewFile();

This will then create myfile.txt if it is not there.
0
JavaJedi012400Author Commented:
Thank you. A good answer that didn't fully answer my question but pointed me in the right direction. Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.