Permission denied while writing file to default Temp folder

My program is quite intensive, so I use a file from scratch to speed things up. I am using the following Java code:

File scratchFile = new File(System.getProperty("java.io.tmpdir") + "WCCTempFile.tmp");
if (!scratchFile.exists())
    scratchFile.createNewFile();

      

This code works great for Mac OS X and Windows. It creates a scratched file in a Java temp directory that is determined by the operating system.

However, when I try this program on Linux (Linux Mint specifically), I get the following error on the line "scratchFile.createNewFile ()"

java.io.IOException: Permission Denied

      

I am really confused about this error because I figured that the temp directory collected by the method System.getProperty("java.io.tempdir")

would be one that the user could write (and it is on other operating systems). Is this not the case on Linux? Is there a way to provide access to the temp directory? Is there another directory that I should be using?

+3


source to share


2 answers


On Linux this is java.io.tmpdir

usually set to a value /tmp

(note the missing trailing /). Instead of fiddling with extra nested forward slashes, it's much easier to use the two parameter file constructor

File scratchFile = new File(System.getProperty("java.io.tmpdir"),"WCCTempFile.tmp");

      



This way you don't have to worry about trailing slashes or not.

+7


source


If you have permission, you can change directory permissions with chmod

.



0


source







All Articles