Почему выбрасывается java.nio.file.AccessDeniedExcepion
Написал небольшую программу для обхода директории и копирования всех файлов определённого типа в другой. (есть недоработка в том, что программа воссоздаёт всё дерево директорий в т.ч. и пустых, но это не важно)
public class Main {
static String fileType;
public static void main(String[] args) throws IOException, InterruptedException {
Scanner in = new Scanner(System.in);
System.out.println("Enter directory name where from you want copy files. You must enter absolute Path");
String sourceStr = in.nextLine();
Path source = Paths.get(sourceStr);
Path destination = Paths.get("D:\\images");
if (!Files.exists(destination)) {
Files.createDirectories(destination);
}
System.out.println("Enter a type of files");
fileType = in.nextLine();
System.out.println(source);
System.out.println(destination);
Files.walkFileTree(source, new MyFileVisitor(source, destination));
System.out.println("Finish");
}
private static String getFileExtension(Path file) {
String fileName = file.getFileName().toString();
System.out.println(fileName);
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
return fileName.substring(fileName.lastIndexOf(".") + 1);
else
return "";
}
static class MyFileVisitor extends SimpleFileVisitor<Path> {
private Path source;
private Path destination;
public MyFileVisitor(Path source, Path destination) {
this.destination = destination;
this.source = source;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
Path newDestination = destination.resolve(source.relativize(dir));
try {
Files.copy(dir, newDestination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
Path newDestination = destination.resolve(source.relativize(file));
if (getFileExtension(newDestination).equals(fileType)) {
try {
Files.copy(file, newDestination, StandardCopyOption.REPLACE_EXISTING);
System.out.println("done");
} catch (IOException e) {
e.printStackTrace();
}
}
return FileVisitResult.CONTINUE;
}
}
}
Проблема в том, что при запуске этой программы у меня на компьютере с версией java 1.8.0_301 программа работает так, как нужно, но при запуске на другом с java 19.0.1 (передал через jar архив) вылезает такой СтекТрейс:
java.nio.file.AccessDeniedException: D:\images
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:235)
at java.base/sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:284)
at java.base/java.nio.file.Files.copy(Files.java:1304)
at IO.CopyFiles.Main$MyFileVisitor.preVisitDirectory(Main.java:60)
at IO.CopyFiles.Main$MyFileVisitor.preVisitDirectory(Main.java:1)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2816)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2881)
at IO.CopyFiles.Main.main(Main.java:32)
и далее при каждом нахождении файла с нужным расширением NoSuchFileException (Вылезает, как я понимаю, из за первой ошибки). В чём проблема - понять не могу, ведь повторюсь, что у меня на компьютере программа работает. Буду очень благодарен за помощь!