【问题标题】:Trying to copy file and getting 'file does not exist' error尝试复制文件并获取“文件不存在”错误
【发布时间】:2026-01-09 23:25:01
【问题描述】:

我在重命名文件后尝试将文件从 1 个目录复制到另一个目录,但一直出现错误:

Exception in thread "main" java.nio.file.NoSuchFileException: C:\Users\talain\Desktop\marketingOriginal\FX Rates\FY17\Q11\Week_12___February_12_2016.xls -> C:\Users\talain\Desktop\fakeOnedrive\FX Rates\FY17\Q11\0

我的代码:

public class shortenFilenameClass 
{
    static String absolutePathLocal =   "C:\\Users\\talain\\Desktop\\marketingOriginal".replace('\\', '/'); //path to original files
    static String absolutePathOnedrive= "C:\\Users\\talain\\Desktop\\fakeOnedrive".replace('\\', '/'); //path to onedrive

        public static void main(String[] args) throws IOException 
        {           
            System.out.println(absolutePathLocal.length());
            File local = new File(absolutePathLocal);
            File onedrive = new File(absolutePathOnedrive);
            int fileCount = 0;

            File[] filesInDir = local.listFiles();
            manipulateFiles(filesInDir, fileCount);
        }

        public static void manipulateFiles(File[] filesInDirPassed, int fileCount) throws IOException
        {
            for(int i = 0; i < filesInDirPassed.length; i++) 
            {
                    File currentFile = filesInDirPassed[i];
                    if(currentFile.isDirectory())
                    {
                        String local = currentFile.getAbsolutePath();
                        //String onedrive = current
                        manipulateFiles(currentFile.listFiles(), fileCount);
                    }
                    String name = currentFile.getName();
                    System.out.println("old filename: " + name);
                    String newName = String.valueOf(fileCount);
                    fileCount++;


                    File oldPath = new File(currentFile.getAbsolutePath());
                    System.out.println("oldPath: " + oldPath);

                    //currentFile.renameTo(new File(oldPath.toString()));
                    System.out.println("currentFile: " + currentFile);

                    String pathExtension = new String(currentFile.getAbsolutePath().substring(42));
                    pathExtension = pathExtension.replaceAll(name, newName);
                    File newPath = new File(absolutePathOnedrive + "/" + pathExtension);
                    System.out.println("newPath: " + newPath);
                    copyFileUsingJava7Files(oldPath, newPath);

                    File finalPath = new File(absolutePathOnedrive + "" + name);
                    //newPath.renameTo(new File(finalPath.toString()));
                    //copyFileUsingJava7Files(newPath, finalPath);
                    System.out.println("renamed: " + name + "to: " + newName + ", copied to one drive, and changed back to original name");
            }
        }


        private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
            Files.copy(source.toPath(), dest.toPath());
        }
}

【问题讨论】:

  • 不,你不是。你得到NoSuchFileException。所有目标目录都存在吗?注意File oldPath = new File(currentFile.getAbsolutePath()); 完全等同于File oldPath = currentFile;

标签: java nio java-io nio2 nosuchfileexception


【解决方案1】:

Java 被告知 C:\Users\talain\Desktop\marketingOriginal\FX Rates\FY17\Q11\Week_12___February_12_2016.xls -&gt; C:\Users\talain\Desktop\fakeOnedrive\FX Rates\FY17\Q11\0 是一个文件,这不是真的。检查错误所在的行,看看你是否在任何地方使用了 lambda 表达式。

【讨论】:

  • 我这样做了。整个消息更有可能来自 Files.copy(),同时显示源和目标,以帮助调试。
  • 这不是这个异常的工作方式。 NoSuchFileException 显示源文件。
最近更新 更多