我使用以下代码来获取路径
Path errorFilePath = FileSystems.getDefault().getPath(errorFile);
Run Code Online (Sandbox Code Playgroud)
当我尝试使用File NIO移动文件时,我收到以下错误:
java.nio.file.InvalidPathException: Illegal char <:> at index 2: \C:\Sample\sample.txt
Run Code Online (Sandbox Code Playgroud)
我也试过使用URL.encode(errorFile)哪个导致同样的错误.
我想将文件从一个包复制到另一个包.
我尝试了Files.copy方法,但它用复制的文件替换了我的文件夹.
public static void main(String[] args) throws IOException {
InputStream in = CopyFileToDirectoryTest.class.getClassLoader()
.getResourceAsStream("com/stackoverflow/main/Movie.class");
Path path = Paths.get("D://folder");
long copy = Files.copy(in, path,StandardCopyOption.REPLACE_EXISTING);
System.out.println(copy);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它删除文件夹并创建文件夹名称的文件.
有没有办法在Java 8或我应该使用apache.commons.io?