Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java文件复制失败_Java - Fatal编程技术网

Java文件复制失败

Java文件复制失败,java,Java,我编写了一个非常简单的Java程序,将作为参数传递的文件复制到/tmp目录。该程序产生几个Java异常 public class CopyFile { public static void main(String[] args) throws IOException { String fqp2File = ""; if (new File(args[0]).isFile()) { fqp2File = args[0]; } else {

我编写了一个非常简单的Java程序,将作为参数传递的文件复制到/tmp目录。该程序产生几个Java异常

public class CopyFile {
public static void main(String[] args) throws IOException {
    String fqp2File = "";

    if (new File(args[0]).isFile()) {
       fqp2File = args[0];
    }
    else {
       System.out.println("Passed argument is not a file");
    }
    copy(fqp2File, "/tmp");
}

private static boolean copy(String from, String to) throws IOException{
    Path src = Paths.get(from);
    Path dest = Paths.get(to);
    try {
        Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
        return true;
    } catch (IOException ioe) {
        System.err.format("I/O Error when copying file");
        ioe.printStackTrace();
        return false;
    }
}
}

运行此程序时,会出现以下错误:

java -jar CopyFile.jar /home/downloads/dfA485MVSZ.ncr.pwgsc.gc.ca.1531160874.13500750
I/O Error when copying filejava.nio.file.FileSystemException: /tmp:
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:103)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:114)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:119)
        at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:578)
        at  sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:265)
        at java.nio.file.Files.copy(Files.java:1285)
        at ca.gc.ssc.gems.esnap.cipo.CopyFile.copy(CopyFile.java:39)
        at ca.gc.ssc.gems.esnap.cipo.CopyFile.main(CopyFile.java:31)

为了测试你的代码,我使用了
C:/tmp/test.txt作为您的
参数[0]
。我修复了这个问题,为输出提供了一个要写入的文件名,如下所示:

Path dest = Paths.get(to);


现在它成功地将文件复制到该名称中,您可以随意修改文件名,或者添加逻辑自动更改文件名。

为了测试代码,我使用了
C:/tmp/test.txt作为您的
参数[0]
。我修复了这个问题,为输出提供了一个要写入的文件名,如下所示:

Path dest = Paths.get(to);


现在它成功地将文件复制到该名称中,您可以随意修改文件名或添加逻辑自动更改文件名。

您尝试传递的参数是什么?您尝试传递的参数是什么?请注意,除了执行
path.get(to+“/test2.txt”)
之外,您还可以使用
path.get(to,“test2.txt”)
让全班同学帮你找出路径分隔符。谢谢,我会编辑它,我想你的意思是
而不加引号!谢谢,作为一个测试,我修改了我的代码以使用
Path dest=Path.get(到“test2.txt”)这是否意味着目录不能用作目标?如果需要,可以修改代码以根据输入名称自动生成名称,这实际上只是使用目录创建文件。不过你需要给它起个名字。请注意,除了使用
path.get(to+“/test2.txt”)
之外,你还可以使用
path.get(to,“test2.txt”)
并让类为你计算路径分隔符。谢谢我编辑它,我假设你的意思是
to
,不带引号!谢谢,作为一个测试,我修改了我的代码以使用
Path dest=Path.get(到“test2.txt”)这是否意味着目录不能用作目标?如果需要,可以修改代码以根据输入名称自动生成名称,这实际上只是使用目录创建文件。不过你得给它起个名字。