File.canWrite()不能按预期工作

我7年前发现了相同的问题,但找不到解决方案。

我创建了具有拒绝写入权限的文件“ readonlyfile.txt”。 Windows不允许 编辑和存储此文件-可以,可以。

File.canWrite()不能按预期工作

但是下面的canWrite()代码表示可以写入此文件。

Path readonlyfile = Paths.get("C:\\temp_\\__readonlyfile.txt"); 
boolean canwrite = false;
try
{   File f = readonlyfile.toFile();
    //File f = new File("C:/temp_/__readonlyfile.txt");
    //File f = new File("C:\\temp_\\__readonlyfile.txt"); 
    canwrite = f.canWrite();   
} catch (Exception e)
{  System.out.println("canWrite()" + e);
}
System.out.println("---canWrite: " + canwrite);  
//---canWrite: true
BufferedWriter bw = null; 
try
{    
bw = Files.newBufferedWriter(readonlyfile);
bw.write("foo");
System.out.println("write() succeeds");

} catch (Exception e) 
{   System.out.println("---cannot open: " + e);
//---cannot open: java.nio.file.accessDeniedException: C:\temp_\__readonlyfile.txt
} finally
{ try { bw.close(); } catch (Exception ee) {}       
}
w5273000 回答:File.canWrite()不能按预期工作

感谢启动引擎盖:Files.isWritable()比File.canWrite()更可靠,但是如果文件不存在,则始终返回false。因此,我在MyFile类中创建了一些代码“ isWritable()”(请参见下文)以解决此问题。测试代码是:

canwrite = MyFile.isWritable(readonlyfile);
System.out.println("canWrite: " + canwrite + " : " + readonlyfile);  

canwrite = MyFile.isWritable(notexists);
System.out.println("canWrite: " + canwrite + " : " + notexists); 

canwrite = MyFile.isWritable(readonlydir);
System.out.println("canWrite: " + canwrite + " : " + readonlydir);

canwrite = MyFile.isWritable(dir); // existing dir
System.out.println("canWrite: " + canwrite + " : " + dir);  

//    canWrite: false : C:\temp_\__readonlyfile.txt
//    canWrite: true : C:\temp_\~~~notexists.txt
//    canWrite: false : C:\temp_\read-only_dir
//    canWrite: true : C:\temp_

/**
 * Similar to Files.isWritable() - however also works for a non-existing file/directory.
 * In this case,the creating of 'path' as a file is simulated to check write access. 
 * Note: Files.isWritable() mistakenly returns false for a non-existing file. 
 * @param path The file or directory to be examined
 * @return true if the caller has write permission to the existing file/directory or could create this non-existing 
 * 'path' as a file,false otherwise
 */
public static boolean isWritable(Path path)
{
    if (path == null) return false;     
try
{
    if (!Files.exists(path))
    {   try 
        { Files.createFile(path); //atomic
          try { Files.delete(path); } catch (Exception e) {}
          return true;
        } catch (AccessDeniedException e) 
        {  return false;
        } catch (Exception e) 
        { logger.log(Level.INFO,"Files.createFile({0}): {1}",new Object[] {path,e}); } // let isWritable() decide
    }       
    return Files.isWritable(path); // works fine for an existing directory too,e.g. read-only-dir

} catch (Exception e)
{  logger.log(Level.WARNING,"MyFile.isWritable(): {0}",e.toString());
}
    return false;
} //--------- end of isWritable()
本文链接:https://www.f2er.com/3051242.html

大家都在问