Monday 1 January 2024

Check whether directory can be accessed and has read and write privileges in Java

In this post, I am going to explain how to check whether a directory is accessed and has read/write privileges.

 

Using below methods of java.io.File class, we can get a solution.

 

public boolean exists()

Return true if and only if the file or directory denoted by this abstract pathname exists, else false.

 

public boolean isDirectory()

Return true if and only if the file denoted by this abstract pathname exists and is a directory, else false.

 

public boolean canRead()

Return true if and only if the file specified by this abstract pathname exists and can be read by the application, else false.

 

public boolean canWrite()

Return true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file, else false.

 

Find the below working application.

 

DirectoryAccessCheck.java

package com.sample.app;

import java.io.File;
import java.io.IOException;

public class DirectoryAccessCheck {

	/**
	 * Checks if a specified directory exists, can be accessed, and has read/write
	 * privileges. If the directory does not exist, it attempts to create it.
	 *
	 * @param dir The {@code File} object representing the directory to be checked.
	 * @throws IOException If the provided {@code File} object is not a directory,
	 *                     if the directory cannot be created, or if the directory
	 *                     lacks read/write privileges. Specific conditions are: -
	 *                     Throws an IOException if the path represented by
	 *                     {@code dir} exists but is not a directory. - Attempts to
	 *                     create the directory if it does not exist, and throws an
	 *                     IOException if this fails. - Throws an IOException if the
	 *                     directory does not have read/write access.
	 */
	public static void checkDirectoryExistAndCanAccess(final File dir) throws IOException {
		if (dir.exists() && !dir.isDirectory()) {
			throw new IOException(dir.getAbsolutePath() + " is not a directory");
		}

		if (!dir.exists()) {
			System.out.println(dir.getAbsolutePath() + " is not exists, creating newly");
			boolean isDirCreated = dir.mkdirs();
			if (!isDirCreated) {
				throw new IOException(dir.getAbsolutePath() + " could not be created");
			}
		}

		if (!(dir.canRead() && dir.canWrite())) {
			throw new IOException(dir.getAbsolutePath() + " directory does not have read/write privilege");
		}
	}

	public static void checkDirectoryExistAndCanAccess(final String dir) throws IOException {
		checkDirectoryExistAndCanAccess(new File(dir));
	}

	public static void main(String[] args) throws IOException {
		String testDir = "/Users/Shared/demo/d1/d2/d3/d4";
		try {
			checkDirectoryExistAndCanAccess(testDir);
			System.out.println("Directory is accessible");
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
}

 


 

 

You may like

file and stream programs in Java

Check given path is a directory and not a symbolic link in Java

Convert InputStream to string

Write InputStream to a file in Java

File separator, separatorChar, pathSeparator, pathSeparatorChar in Java

Implement an Output stream that writes the data to two output streams

No comments:

Post a Comment