我已经搜索过这个答案,我也尝试过解决这个问题,但我做不到。我得到了异常类型
No fue posible copiar los archivos. Motivo: C:\Test1 (Acceso denegado)java.io.FileNotFoundException: C:\Test1 (Acceso denegado)
java.io.FileNotFoundException: C:\Test1 (Acceso denegado)
翻译成“无法复制文件。动机:(访问被拒绝)”。
我想做的是递归地将一个列表复制到一个目录中。
我可以简单地递归地复制文件(我已经这样做了),但要求是将所有文件复制到一个列表中,然后对列表中的记录执行任何我想做的事情(复制、删除等)。
我的列表包含这些记录:
C:\Test\Carpeta_A
C:\Test\Carpeta_A\Entrenamiento_1.txt
C:\Test\Carpeta_A\Requerimientos.txt
C:\Test\Carpeta_B
C:\Test\Carpeta_B\queries.txt
C:\Test\Things.txt
这是我的代码:
这是主要方法..它调用一个方法来列出和保存文件和目录,然后调用将文件复制到另一个目录的方法,保留我的主要结构:
public static void main(String[] args) throws IOException
{
String fuente = "C:/Test";
String ruta = "C:/Test1";
teeeeeest listing = new teeeeeest();
List<File> files = listing.getFileListing(fuente);
listing.copyDirectories(files, ruta);
}
public List<File> getFileListing( String fuente ) throws FileNotFoundException
{
List<File> result = getFileListingNoSort(fuente);
Collections.sort(result);
return result;
}
private List<File> getFileListingNoSort( String fuente ) throws FileNotFoundException
{
File source = new File(fuente);
List<File> result = new ArrayList<>();
File[] filesAndDirs = source.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs) {
result.add(file); //always add, even if directory
String s = file.getPath().trim();
if (! file.isFile()) {
//must be a directory
//recursive call!
List<File> deeperList = getFileListingNoSort(s);
result.addAll(deeperList);
}
}
return result;
}
public static void copyDirectories(List<File> files, String destiny)
throws IOException
{
InputStream in = null;
OutputStream out = null;
File targetPath = new File(destiny);
System.out.println(targetPath.getPath());
for(int i = 0; i < files.size(); i++)
{
File temp = new File(files.get(i).toString());
//System.out.println(temp.getPath());
try
{
if(temp.isDirectory())
{
if(!targetPath.exists())
{
targetPath.mkdir();
}
File[] filesAndDirs = temp.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs)
{
if (! file.isFile())
{
//must be a directory
//recursive call!
copyDirectories(filesDirs,destiny);
}
}
}
else
{
in = new FileInputStream(files.get(i).toString());
out = new FileOutputStream(targetPath);
System.out.println(temp.getPath());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
}
}
catch(Exception e)
{
System.err.println("No fue posible copiar los archivos. Motivo: " + e.getMessage() + e);
e.printStackTrace();
}
}
}
最佳答案
如果我只是把工作代码贴在这里会很糟糕,它不会帮助你思考问题是什么。我会尽量给你足够的,而不是全部:如果对你有帮助,请标记我的答案为已接受。
1:您不应该对文件列表进行排序。读取文件的顺序很重要,这样您就不会在列表中的目录之前获取文件。也就是说,您是否对它们进行排序并不重要,因为较短的名称(目录)无论如何都会首先出现。尽管如此,不要做你不应该做的工作。删除 getFileListing 方法并仅使用 getFileListingNoSort。
List<File> files = listing.getFileListingNoSort(fuente);
2:您需要将源目录和目标目录都传递给 copyDirectories,以便您可以从源文件名创建目标文件名。
listing.copyDirectories(files, fuente, ruta);
3:您需要从源文件名创建一个目标文件。可能有更好的方法,但使用简单的字符串解析就可以解决问题:
File temp = files.get(i);
String destFileName = destiny + temp.toString().substring(source.length());
File destFile = new File(destFileName);
4:您必须根据新的 destFile 创建新目录。您正在使用 targetPath,它只是基本目录,而不是需要创建的新目录。
if(!destFile.exists())
{
destFile.mkdir();
}
5:创建目标目录后,就没有其他事情可做了。删除之后的所有代码,直到“else”
6:您的 outfile 应该是您创建的新 destFile。
out = new FileOutputStream(destFile);
7: 关闭你的输入和输出流,否则文件副本将不完整。
in.close();
out.close();
这应该让你继续。如果可以,请使用 IDE,这样您就可以使用调试器单步执行程序并查看发生了什么。
关于java - 将保存的文件从 List<File> 递归复制到 java 中的目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579507/