【问题标题】:Is it possible to use a String as the arguments for Files.move(source,target..) in Java? [closed]是否可以在 Java 中使用 String 作为 Files.move(source,target..) 的参数? [关闭]
【发布时间】:2012-09-05 15:32:54
【问题描述】:

你能否以某种方式使用字符串来定义 files.move 的源和目标。

这是文档http://docs.oracle.com/javase/tutorial/essential/io/move.html

【问题讨论】:

  • 这个类的 Javadoc 是怎么说的? File 没有 move() 方法,但 Files 有;)

标签: java string file move


【解决方案1】:

根据javadoc,您不能使用字符串作为 Files.move 的参数。

对您来说似乎更好的解决方案是在 File.rename 上使用 rename 方法。像这样的:

File file = new File("/path/to/file/to/be/moved");
boolean moved = file.renameTo("/new/path/for/the/file");
if(!moved)
  //Handle the error

【讨论】:

    【解决方案2】:

    简短的回答是Files.move 需要Path 对象。也就是说,您可以使用Paths.get(str) 简单地将String 转换为Path

    【讨论】:

    • 谢谢你这就是我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 2020-08-12
    • 2013-11-13
    • 2014-05-30
    • 2021-10-22
    • 2022-11-21
    相关资源
    最近更新 更多