gpt4 book ai didi

java - 如何读取文件夹、计算文件数量并复制到新文件夹

转载 作者:行者123 更新时间:2023-12-01 05:03:30 25 4
gpt4 key购买 nike

我上一个问题的后续 - Java: How to read directory folder, count and display no of files and copy to another folder? ,我想读取一个文件夹,统计该文件夹中的文件(任何文件类型),显示文件数量,然后复制到一个新文件夹中。但是,我遇到以下异常:

Exception in thread "main" java.io.FileNotFoundException: C:\project\curFolder (Access is denied) C:\project\newFolder already exist: Continuing with process! at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:120) at filetransfer.FileTransfer.copyFiles(FileTransfer.java:54) at filetransfer.FileTransfer.checkDir(FileTransfer.java:44) at filetransfer.FileTransfer.readDirectory(FileTransfer.java:29) at filetransfer.FileTransfer.main(FileTransfer.java:12) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

请记住我是一名学生。这是我到目前为止所做的:

public class FileTransfer {

public static void main(String[] args) throws FileNotFoundException, IOException {
readDirectory();
}

public static void readDirectory() throws FileNotFoundException, IOException {
//create new file object with location of folder
File curFolder = new File("C:/project/curFolder/");
int totalFiles = 0;
//for loop to count the files in the directory using listfiles method
for (File file : curFolder.listFiles()) {
//determine if the file object is a file
if (file.isFile()) {
//count files ++
totalFiles++;
}
}
//display number of files in directory
System.out.println("Number of files: " + totalFiles);
checkDir();
}

public static void checkDir() throws FileNotFoundException, IOException {
//check if destination directory exist, if not create directory
//create new file object with copy folder destination
File newFolder = new File("C:/project/newFolder/");
//Check if folder exist: True: Println with message(true and continuing)
if (newFolder.exists()) {
System.out.println(newFolder + " already exist: Continuing with process!");
} else {
//False: Create Dir
newFolder.mkdir();
System.out.println(newFolder + " created!");
}
copyFiles();
}

public static void copyFiles() throws FileNotFoundException, IOException {
//copy files from specified directory to new directory
File fromCur = new File("C:/project/curFolder/");
File toNew = new File("C:/project/newFolder/");
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(fromCur);
to = new FileOutputStream(toNew);
byte[] buffer = new byte[4096];
int bytesRead;

while ((bytesRead = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesRead);
}
} finally {
if (from != null) {
try {
from.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
if (to != null) {
try {
to.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
}

最佳答案

错误在于以下行:

from = new FileInputStream(fromCur);

因为C:\project\curFolder是一个文件夹而不是文件。根据FileInputStream,您看到的异常是正确的文档。

Throws: FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.

(我在上面引用中强调)

我相信您可能想要检查输入文件 is a directory然后list the files并将它们一一复制。

关于java - 如何读取文件夹、计算文件数量并复制到新文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13046757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com