将 excel 文件复制到根据 java 中的当前日期创建的文件夹

Copy excel file to folder created based on current date in java

项目需要将 excel 文件从源复制到基于当前日期创建的目标文件夹。我能够创建当前日期和时间文件夹,但无法将文件复制到那里。获取错误消息 "Access Denied"

public static void writeRequestAndResponse() throws IOException {   


        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

        String currentDateTime = format.format(date);

        String folderPath = "E:\QA\Output\" + currentDateTime ;

        File theDir = new File(folderPath);

        // if the directory does not exist, create it
        if (!theDir.exists()) {
            System.out.println("creating directory: " + theDir.getName());
            boolean result = false;          


            try {

                theDir.mkdirs();
                result = true;
               final String folderpath1 = folderPath + "\test.api\" + "\exceloutput";
                File theDir1 = new File(folderpath1);
                theDir1.mkdirs();
                System.out.println(folderpath1);

                String frompath = "E:\Project\src\main\java\com\qa\testdata\APITestData.xlsx";
                //FileInputStream is = new FileInputStream(frompath);
                File file1 = new File(frompath);

                //String str1="E:\QA\Output\20200121172737\tc.api\exceloutput";

               // File file2 = new File(str1);
              final String topath=folderpath1;

                File file2 = new File(topath);
                //PrintWriter out = new PrintWriter(new FileOutputStream(topath));
                //FileOutputStream outfs=  new FileOutputStream(topath);
                Files.copy(file1,file2);


            }
            catch (SecurityException se) {
                // handle it
                System.out.println(se.getMessage());
            }
            if (result) {
                System.out.println("Folder created");
            }
        } else if (theDir.exists()) {

            System.out.println("Folder exist");
        }


       }

控制台中的错误消息显示为 "java.io.FileNotFoundException: E:\QA\Output200122094149\test.api\exceloutput (Access is denied)"

请确保 excel 文件存在于同一路径上并且该文件具有访问权限。尝试手动复制文件并检查

我无法确定您使用的 Java 版本。因此,我使用 Java 8。然后通过这 2 个更改,它正在工作。

  1. Files.copy() 在 Java 8 中获取 Path 个实例,而不是 File 个实例。所以,改变那个。
  2. Files.copy() 中,目标 Path 应该指向文件而不是文件夹。

更改代码(删除注释代码):

public static void writeRequestAndResponse(){

    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );

    String currentDateTime = format.format( date );

    String folderPath = "E:\QA\Output\" + currentDateTime;

    File theDir = new File( folderPath );

    // if the directory does not exist, create it
    if (!theDir.exists()) {
        System.out.println( "creating directory: " + theDir.getName() );
        boolean result = false;

        try {

            theDir.mkdirs();
            result = true;
            final String folderpath1 = folderPath + "\test.api\" + "\exceloutput";
            File theDir1 = new File( folderpath1 );
            theDir1.mkdirs();
            System.out.println( folderpath1 );

            String frompath = "E:\Project\src\main\java\com\qa\testdata\APITestData.xlsx";
            File file1 = new File( frompath );

            final String topath = folderpath1 + "\outputFile.xlsx"; //The output file name included

            File file2 = new File( topath );
            Files.copy( file1.toPath(), file2.toPath() );
        } catch (Exception se) {
            // handle it
            System.out.println( se.getMessage() );
        }
        if (result) {
            System.out.println( "Folder created" );
        }
    } else if (theDir.exists()) {

        System.out.println( "Folder exist" );
    }

}