Java将文件从一个文件夹复制到另一个文件夹并更改名称

huangapple go评论66阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月23日 19:43:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64027203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定