英文:
Java copy file from folder to another folder with different name
问题
我使用我的源路径和目标路径配置了属性文件:
pathSource = C://Test
pathOut = C://Test//Folder
我尝试将文件从pathSource
复制到pathOut
(使用变量config.getValue()
):
Files.copy(config.getValue("pathTemplate"), config.getValue("pathOut"), REPLACE_EXISTING);
我的两个config
变量都是字符串,为什么会出现这个错误:无法解析方法 'copy(java.lang.String, java.lang.String, java.nio.file.StandardCopyOption)
。
英文:
I configured my properties file with my source path and dest path :
pathSource = C://Test
pathOut = C://Test//Folder
I try to copy this file from pathSource to pathOut (with variable config.getValue()
:
Files.copy(config.getValue("pathTemplate"), config.getValue("pathOut"), REPLACE_EXISTING);
My 2 variables config are String why I have this error : Cannot resolve method 'copy(java.lang.String, java.lang.String, java.nio.file.StandardCopyOption)
答案1
得分: 2
Files.copy()
要求前两个参数的类型为 Path
。
您需要根据您的字符串构造这些参数,如下所示:
Path input = Paths.get(config.getValue("pathTemplate"));
Path output = Paths.get(config.getValue("pathOut"));
Files.copy(input, output, REPLACE_EXISTING);
英文:
Files.copy()
requires the first two parameters to be of type Path
.
You need to construct those from your strings i.e.
Path input = Paths.get(config.getValue("pathTemplate"));
Path output = Paths.get(config.getValue("pathOut"));
Files.copy(input, output, REPLACE_EXISTING);
答案2
得分: 0
Files.copy方法不接受String参数。它接受Path参数。请参阅文档:https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html
您需要将config.getValue更改为返回Path对象,或将String输出转换为Path对象。
英文:
Files.copy method does not accept String parameters. It accepts Path parameters. Please see the documentaion: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html
You will have to change your config.getValue to return a Path object or convert the String output into Path objects
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论