Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checks if directory exists and readable
import java.io.File;
public class DirectoryChecker
{
public static boolean isWindows()
{
String os = System.getProperty("os.name").toLowerCase();
return os.indexOf("windows") != -1;
}
public static boolean canRead(File directory)
{
if (!isWindows())
return directory.canRead();
File testFolder = new File(directory, "TestFolder");
return testFolder.mkdir() && testFolder.delete();
}
public static void main(String[] args)
{
String dirName = args.length == 0 ? "." : args[0];
File directory = new File(dirName);
System.out.println(dirName + " exists: " + directory.exists());
System.out.println(dirName + " is directory: " + directory.isDirectory());
System.out.println(dirName + " can be read: " + canRead(directory));
}
}
@DNNX

This comment has been minimized.

Copy link
Owner Author

DNNX commented Jun 29, 2011

Compile:
javac DirectoryChecker.java

Run:
java DirectoryChecker "\\ipaddress\path\to\your\dir\"
or
java DirectoryChecker "c:\test"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.