简体   繁体   中英

How do you copy a file in java using Apache Commons IO with a custom name?

I was just playing around w/ java and Mahout and I ran into this error while coding.

I'm trying to copy a file in java with apache, but it shows:

  Exception in thread "main" java.io.IOException: Destination 'algorithmResDump\item2019\09\20:22' directory cannot be created
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1070)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1028)
at com.predictionmarketing.itemrecommend.copyFile.copyFile(copyFile.java:14)
at com.predictionmarketing.itemrecommend.UserBasedRecommender.main(UserBasedRecommender.java:93)'

Some snippets of the code:

package com.predictionmarketing.itemrecommend;

import org.apache.commons.io.*;

import java.io.File;
import java.io.IOException;

public class copyFile {
    public static void copyFile(String source1, String dest1, String filename) throws IOException {

        File source = new File (source1);
        File dest = new File (dest1, filename);

        FileUtils.copyFile(source, dest);
    }

}

-Naming of the output file

Date dNow = new Date();
SimpleDateFormat ftRaw = new SimpleDateFormat ("yyyy/MM/dd:HH//mm:ss.SSS");
String ft1 = ftRaw.format(dNow);
copyFile.copyFile("data/send.data", "algorithmResDump/", "item" + ft1 + ".data");

I'm confused by how Apache thinks it is a directory instead of a file it even says 'FileUtils.copyFile' A little bit misleading there. Any help would be appreciated!

You have '/' in your SimpleDateFormatter, due to this apache fileutils will try to create directory for year, month, day:hour etc.

But the issue is you have ':' for date and hour, fileUtils will try to create a directory named "22:20" but in windows ':' is a illegal character, so fileutils will fail by throwing unable to create directory.

Instead of having '/' (or) ':', if your prefer having timestamp, i would suggest format like "YYYY-mm-dd_HH-MM-SS" before this doesn't have any illegal characters in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM