爪哇 - 将文件复制到使用文件实用和copyFileToDirectory另一个目录 - 不工作? - [英] Java - Copy file to another directory using FileUtils and copyFileToDirectory - doesn't work -?

查看:1570
本文介绍了爪哇 - 将文件复制到使用文件实用和copyFileToDirectory另一个目录 - 不工作? - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个文件从一个目录复制到Apache组织公地的另一个采用Java语言和文件实用程序类。

我写了一个快速的java程序来测试我的本地系统上。这里是code。该文件存在,但文件到另一个目录的复制无法正常工作。我在想什么?有一些语法不当的地方?

 进口org.apache.commons.io.FileUtils;
进口的java.io.File;类MoveFile {    公共静态无效的主要(字串[] args){
        MoveFile MyObj中=新MoveFile();
        myobj.moveTheFile();
    }    公共无效moveTheFile(){
        尝试{
            文件DESTDIR =新的文件(C:\\\\ \\\\ Folder1中TEMP2);
            文件SRCFILE =新的文件(C:\\\\ \\\\ Folder1中临时\\\\ card.png);
            FileUtils.copyFileToDirectory(SRCFILE,DESTDIR);
        }赶上(例外五){
        }
    }}


解决方案

复制您的错误,它不但不能当程序没有权限在目标文件夹的写。即使捉不到抛出和打印堆栈跟踪显示没有资料和方法是相当无语......如果文件夹不存在,该方法将创建它,无视可能的修正。

在目标文件夹中检查写入权限

I would like to copy a file from one directory to another using Java and the FileUtils classes of apache org commons.

I wrote up a quick java program to test on my local system. Here is the code. The file exists, but the copying of the file to another directory isn't working. What am I missing? Is there some improper syntax somewhere?

import org.apache.commons.io.FileUtils;
import java.io.File;

class MoveFile {

    public static void main(String[] args) {
        MoveFile myobj = new MoveFile();
        myobj.moveTheFile();
    }

    public void moveTheFile () {
        try {
            File destDir = new File("C:\\Folder1\\temp2");
            File srcFile = new File("C:\\Folder1\\temp\\card.png");
            FileUtils.copyFileToDirectory(srcFile, destDir);
        } catch(Exception e) {
        }
    }

}

解决方案

Replicated your error and it only fails when the program does not have permission to write on destination folder. Even catching a throwable and printing stacktrace shows no info and the method is quite silent... if the folder does not exist, the method creates it so disregard that possible correction.

Check write permissions in destination folder

这篇关于爪哇 - 将文件复制到使用文件实用和copyFileToDirectory另一个目录 - 不工作? - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆