Java 8 如何将文件复制到另一个目录?
How to copy file into another directory in Java 8?
我想将文件从一个包复制到另一个包。
我尝试了 Files.copy
方法,但它用复制的文件替换了我的文件夹。
public static void main(String[] args) throws IOException {
InputStream in = CopyFileToDirectoryTest.class.getClassLoader()
.getResourceAsStream("com/Whosebug/main/Movie.class");
Path path = Paths.get("D://folder");
long copy = Files.copy(in, path,StandardCopyOption.REPLACE_EXISTING);
System.out.println(copy);
}
这不起作用,因为它会删除文件夹并使用该文件夹的名称创建文件。
在 Java 8 中有没有办法,或者我应该使用 Apache Commons IO?
Files.copy
需要目标文件的名称。
Path targetFilePath = Paths.get("D:/folder/Movie.class");
这确实比常规要求多一点"if the target is a directory, copy the file into it." 另一方面,一个非常有用的要求:InputStream 不再有名称。
我想将文件从一个包复制到另一个包。
我尝试了 Files.copy
方法,但它用复制的文件替换了我的文件夹。
public static void main(String[] args) throws IOException {
InputStream in = CopyFileToDirectoryTest.class.getClassLoader()
.getResourceAsStream("com/Whosebug/main/Movie.class");
Path path = Paths.get("D://folder");
long copy = Files.copy(in, path,StandardCopyOption.REPLACE_EXISTING);
System.out.println(copy);
}
这不起作用,因为它会删除文件夹并使用该文件夹的名称创建文件。
在 Java 8 中有没有办法,或者我应该使用 Apache Commons IO?
Files.copy
需要目标文件的名称。
Path targetFilePath = Paths.get("D:/folder/Movie.class");
这确实比常规要求多一点"if the target is a directory, copy the file into it." 另一方面,一个非常有用的要求:InputStream 不再有名称。