sutoL
sutoL

Reputation: 1767

Creating files for later usage in java

I am writing a Java application that writes a number of files, and later reads the files for their values. I am using InstallShield to install the application under C:\Program Files, and this is where the temporary files usually get created. However, when using Windows 7, the files are created under the users temporary folder instead, with a random name.

Here is my code...

File usersTemp = File.createTempFile("users", null,temp);

And this is the file that gets generated...

C:\Users\TP\AppData\Local\Temp\users2343200092608531612.tmp

As the file is generated with a random number, it makes it hard to retrieve the file back for processing. Is there a better way to do this?

Upvotes: 0

Views: 167

Answers (3)

Brendan Long
Brendan Long

Reputation: 54242

Unless you're running as a superuser (which you're probably not), you can only edit files in the user's home directory, which you can find with the user.home system property:

System.getProperty("user.home")

So, if you wanted to save a file in your application's application data folder:

File appDataFolder = new File(System.getProperty("user.home"), "Local Settings\\Application Data\\YourProgramName");

Then save things under that folder and they will stick around.

Upvotes: 2

wattostudios
wattostudios

Reputation: 8764

The definition of a Temp file is that it should only exist for a short period of time. It seems like you are wanting to create a file that will persist for a longer time, possibly remaining between runs of the application? If this is the case, you should be creating a file with a proper name...

new File("c:\filename.txt").createNewFile();

That way, you are able to choose a directory and a name that would be suitable, no matter what operating system you're running the application on.

Upvotes: 1

lrAndroid
lrAndroid

Reputation: 2854

Is there some reason why you can't just save the files normally (naming them whatever you want)? I've written many Java programs for Windows 7 and haven't had any problems saving files.

Just use a FileOutputStream or whatever method you usually use to save files.

Upvotes: 1

Related Questions