Issue
I need to create some directories and files and all of them should have permission 0600.
When I Run from NetBeans Debug:
After creating directory when i try to store some files there I get IOException
with "Permission Denied" message while both directories and files are created with same application at the same time with the same user so I think 0600 (owner read/write) should work.
And When Running Jar file, chmod doesn't work at all!
My code is:
if(!Dest.exists()){
boolean res=dirs.mkdirs();
if(res){
Runtime.getRuntime().exec("chmod -R 600 '"+dirs.getAbsolutePath()+"'");
}
}
File Destination=new File(Dest, source.getName());
documentManager.copyFile(source, Destination);
and the copyFile is:
public static void copyFile(File sourceFile, File destFile) throws FileNotFoundException,IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
Runtime.getRuntime().exec("chmod 600 '"+destFile.getAbsolutePath()+"'");
destination.close();
}
}
}
What's the problem?
Thanks
Solution
The directory needs to have the executable bit set for you to write files into it. Try chmod +x on the directory.
mkdir tmp2323
chmod a-x tmp2323
touch tmp2323/test
touch: cannot touch `tmp2323/test': Permission denied
Answered By - jontro Answer Checked By - Clifford M. (WPSolving Volunteer)