Updated February 9, 2023
Definition of Java nio file AccessDeniedException
Java nio file accessdeniedexception is thrown when the file system denied the operation, it is due to the access check or permission issue on file. The java nio file exception is a very common issue, this exception also occurs when the file directory will not contain the appropriate privilege of users. We can also use AccessDeniedException class to check if the exception is thrown due to the file permission issue.
Overview of Java nio file AccessDeniedException
In java, the nio file access denied exception is thrown when the file system denied the operation to view or edit the file. To solve this error, we need to check the permission of the file. Those exceptions are not related to the security exception or access controlled exception, which is thrown to access security managers or controllers at the time file access was denied.
The nio file package uses static methods to operate on the directories and files. To use the java nio file we have required permission on it. Like we need to check the file will contain read permission before read any contents from the file, if we need to write into the file then we need to check file contains write permission before anything is written into the file.
Key Takeaways
- To handle the java nio file access exception we use the class of AccessDeniedException. This class is useful to handle the exception of the java nio file.
- We use multiple methods and constructors to throw the exception of java nio access denied, we can also check the delegate API.
Java nio file AccessDeniedException – File System
The java nio file access denied exception occurs due to the file system. If the file does not contain appropriate permission, then it shows the access denied exception. The below example shows how file access denied exception occurs due to the file system. Below we need to import the class of AccessDeniedException. Also, we need to import the Path, Files, and Paths packages as follows.
Code:
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class java_niofile {
public static void main(String[] args) throws IOException {
Path f = Paths.get ("C:\\Program Files\\Java\\jdk1.8.0_351\\jmc.txt");
String um = "";
um = strategyA (f);
System.out.println ("Strategy A: " + um);
um = strategyB(f);
System.out.println ("Strategy B: " + um);
}
public static String strategyA(Path f) throws IOException {
String um = "";
boolean iw = Files.isWritable (f);
if (iw) {
Files.delete (f);
um = "Deleted";
} else {
um = "…..";
}
return um;
}
public static String strategyB (Path f) throws IOException {
String um = "";
try {
Files.delete(f);
um = "Deleted";
} catch (AccessDeniedException ade) {
ade.printStackTrace ();
um = ade.getMessage () + " This file is not writable.";
}
return um;
}
}
In the above example, we can see that the code throws the exception due to we have not to access the file. But in the below example, we can see that we have used the same code, and used different files. In the below example, our execution is successful. The file is successfully deleted. Also, a message shows that no such file exception.
Code:
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class java_niofile {
public static void main(String[] args) throws IOException {
Path f = Paths.get ("G:\\ file.txt");
String um = "";
um = strategyA (f);
System.out.println ("Strategy A: " + um);
um = strategyB(f);
System.out.println ("Strategy B: " + um);
}
public static String strategyA(Path f) throws IOException {
String um = "";
boolean iw = Files.isWritable(f);
if (iw) {
Files.delete (f);
um = "Deleted";
} else {
um = "….";
}
return um;
}
public static String strategyB(Path f) throws IOException {
String um = "";
try {
Files.delete(f);
um = "Deleted";
} catch (AccessDeniedException ade) {
ade.printStackTrace();
um = ade.getMessage() + " This file is not writable.";
}
return um;
}
}
Error and Solution
The java nio file access denied exception error occurs because we do not have permission to access the specified file. The most common cause of the access denied error is that we have not specified privileges on that file. To check the permission of the file we can execute the below code as follows.
Code:
package java_niofile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class java_niofile {
public static void main(String[] args) {
Path f = Paths.get("C:\\Program Files\\Java\\jdk1.8.0_351\\jmc.txt");
boolean irf = Files.isRegularFile(f);
boolean ih = Files.isReadable(f);
boolean ir = Files.isReadable(f);
boolean ie = Files.isExecutable(f);
boolean isl = Files.isSymbolicLink (f);
Path dir = Paths.get ("C:\\Program Files\\Java\\jdk1.8.0_351\\");
boolean id = Files.isDirectory (dir);
boolean iw = Files.isWritable(dir);
}
}
The below example shows the AccessDeniedException error as follows. In below example file does not contain the appropriate permission so it gives the error of access denied as follows.
Code:
package java_niofile;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class java_niofile {
public static void main(String[] args) throws IOException {
Path f = Paths.get("C:\\Program Files\\Java\\jdk1.8.0_351\\jmc.txt");
String um = "";
um = stA(f);
System.out.println("Strategy B: " + um);
}
public static String stA(Path f) throws IOException {
String um = "";
try {
Files.delete(f);
um = "Deleted";
} catch (AccessDeniedException ade) {
ade.printStackTrace();
um = ade.getMessage() + " This file is not writable.";
}
return um;
}
}
The solution to the java nio file access denied exception error is to use the file that contains appropriate access and privileges. The below example shows the solution to the access denied exception error.
Code:
package java_niofile;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class java_niofile {
public static void main(String[] args) throws IOException {
Path f = Paths.get("G:\\file1.txt");
String um = "";
um = stA(f);
System.out.println("Strategy B: " + um);
}
public static String stA(Path f) throws IOException {
String um = "";
try {
Files.delete(f);
um = "Deleted";
} catch (AccessDeniedException ade) {
ade.printStackTrace();
um = ade.getMessage() + " This file is not writable.";
}
return um;
}
}
Delayed File Deletion on Windows
We can experience the issue of delayed file deletion on windows. This exception is now thrown due to the illegal access of the file, but it was thrown due to the lock on to the specified file. If someone uses the same file and same time we attempt to delete this file, then windows OS locks that specified file and we have received the exception of delayed deletion of the file.
The steps of delayed file deletion on windows are as follows:
1. In the first step we need to submit the request to delete the lock file.
2. The control is returned from the method of actual file deletion that was delayed from windows.
3. At the time when the lock file is removed, another process of java for trying to create the exception of access denied.
Code:
package java_niofile;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
public class java_niofile {
public static void main(String[] args) throws IOException
{
new java_niofile().execute();
}
public void execute() throws IOException{
File d = new File("G:\\file\\test");
d.mkdirs();
char[] data = new char[100];
Arrays.fill(data, (char)'q');
for(int x = 0; x < 1760; x++)
{
File f = new File(d, "G:\\file" + x);
f.createNewFile();
FileWriter fw = null;
try{
fw = new FileWriter(f);
fw.write(data);
}finally{
if(fw != null){
fw.close();
}
}
}
delete(d);
d.mkdirs();
new File(d, "f").createNewFile();
}
private void delete (File f){
if(f.isDirectory()){
for(File afile : f.listFiles())
{
delete(afile);
}
f.delete();
}else{
f.delete();
}
}
}
During IO.createDirectory
In Java, we use the mkdir function to create a new data directory. This method of java is used to take the parameter abstract path name and also defined the same into the file class of java. The mkdir function returns true if the directory is created without any error. If any error has occurred during directory creation, then it will return false in output. The java.io file class is used to create a new directory in java.
The below example shows to create the directory in java as follows:
Code:
package java_niofile;
import java.io.*;
class java_niofile {
public static void main(String[] args)
{
File f = new File("G:\\file");
if (f.mkdir() == true) {
System.out.println("Directory created");
}
else {
System.out.println("Directory not created");
}
}
}
Conclusion
The java nio file access denied exception is a very common issue, this exception also occurs when the file directory will not contain the appropriate privilege of users. The nio file package uses static methods to operate on the directories and files. To use the java nio file we have required permission on it.
Recommended Articles
This is a guide to Java nio file AccessDeniedException. Here we discuss the introduction, file system, error and solution, and delayed file deletion on windows. You can also look at the following articles to learn more –