如何检查给定的本地路径是否可以在java中写入

时间:2014-02-04 22:21:24

标签: java file io

我想查看以下内容

A)路径类型:本地:C:\ program files \ cts \ ABCD_PP \ dd \

1)检查路径是否存在 2)如果路径存在,则检查路径是否可写(意味着应该能够在该路径上放置文件)。

B)共享路径:\\ mdyptcmedia101 \ transmit \ in \

它是与本地文件路径验证相同的程序还是不同的

在春季项目中使用Jdk 1.6。

请帮忙。

4 个答案:

答案 0 :(得分:6)

答案 1 :(得分:2)

File file = new File("...");
boolean canWrite = file.canWrite();
boolean isDirectory = file.isDirectory();

javadoc

中详细说明

答案 2 :(得分:1)

这很简单。

String path = "..."; // Whatever the path may be
File file = new File(path);

if (file.isDirectory()) {
   // The path exists and it is a directory
   if (file.canWrite() {
       // The directory is writable
   } else {
       // The directory is not writable
   }
} else {
   // The path is not a directory, or it does not even exist
}

答案 3 :(得分:0)

File I/O - Checking a File or Directory

Path path = ...;
if (Files.exists(path))
    /* the path exists */;

if (Files.isDirectory(path))
    /* the path exists and is a directory */;

if (Files.isWritable(path))
    /* the path is writable */;