0

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?

2
  • Can't you set the file name in some variable by calling usersTemp.getAbsolutePath() and use that string for later? Commented Apr 11, 2012 at 2:40
  • Yes, i tried that however if i close the program and run it again i do not have the variable already, it would be replaced with a new one. Commented Apr 11, 2012 at 2:44

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

the problem is my program is unable to create files under the C:\Program Files directory , and it is because of administrative rights if i am not wrong
1

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.

2 Comments

hi , that is exactly what i think would work, however due to windows adminstrative rights i would get a java.io.IOException: Access is denied
You would need to choose a directory that the user has permissions to write to. Probably the only guaranteed way to do this is to use the users home directory, as suggested by @Brendan... System.getProperty("user.home"). All other directories potentially forbid write access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.