首页 \ 问答 \ Files.copy(Path,Path)是否创建目录?(Does Files.copy(Path,Path) create directories?)

Files.copy(Path,Path)是否创建目录?(Does Files.copy(Path,Path) create directories?)

我在Java程序( C:/Users/java/dir1 )的目录下有一堆文本文件(比如ss1.txt,ss2.txt,ss3.txt等)?
我想将我的txt文件移动到尚未创建的新目录。 我有一个所有文件的字符串地址,我想我可以使用它们将它们转换为路径

路径路径= Paths.get(textPath);

将创建一个String( C:/Users/java/dir2 ),使用上面的方法将其转换为路径,然后使用

Files.copy(C:/Users/java/dir1/ss1.txt,C:/用户/爪哇/ DIR2)

导致ss1.text被复制到新目录?


I have a bunch of text files(say ss1.txt,ss2.txt,ss3.txt etc.) under a directory with my Java program (C:/Users/java/dir1)?
I want to move my txt files to a new directory that hasn't been created yet. I have a String address for all of my files and I think I can turn them into Paths using

Path path = Paths.get(textPath);

Would creating a String (C:/Users/java/dir2), turning that into a path using the above method and then using

Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)

result in ss1.text being copied to a new directory?


原文:https://stackoverflow.com/questions/29597454
更新时间:2020-10-25 21:10

最满意答案

方法Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)不会创建目录,它将在目录java中创建包含ss1.txt数据的文件dir2。

您可以使用以下代码尝试:

File sourceFile = new File( "C:/Users/java/dir1/ss1.txt" );
Path sourcePath = sourceFile.toPath();

File destFile = new File( "C:/Users/java/dir2" );
Path destPath = destFile.toPath();

Files.copy( sourcePath, destPath );

请记住使用java.nio.file.Files和java.nio.file.Path。

如果要使用类表单java.nio将文件从一个目录复制到另一个目录,则应使用Files.walkFileTree(...)方法。 您可以在此处查看解决方案Java:使用nio Files.copy移动目录

或者你可以简单地使用来自apache http://commons.apache.org/proper/commons-io/库的`FileUtils类,从版本1.2开始提供。

File source = new File("C:/Users/java/dir1");
File dest = new File("C:/Users/java/dir2");
try {
    FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
    e.printStackTrace();
}

Method Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2) will not create directory, it will create file dir2 in directory java that will contain ss1.txt data.

You could try it with this code:

File sourceFile = new File( "C:/Users/java/dir1/ss1.txt" );
Path sourcePath = sourceFile.toPath();

File destFile = new File( "C:/Users/java/dir2" );
Path destPath = destFile.toPath();

Files.copy( sourcePath, destPath );

Remember use java.nio.file.Files and java.nio.file.Path.

If you want to use class form java.nio to copy files from one directory to other you should use Files.walkFileTree(...) method. You can see solution here Java: Using nio Files.copy to Move Directory.

Or you can simply use `FileUtils class from apache http://commons.apache.org/proper/commons-io/ library, available since version 1.2.

File source = new File("C:/Users/java/dir1");
File dest = new File("C:/Users/java/dir2");
try {
    FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
    e.printStackTrace();
}
2017-05-23

相关文章

更多

最新问答

更多
  • 如何保护Solr只允许SELECT请求给用户并禁止其他任何东西?(How to secure Solr to allow SELECT request to only users and disallow anything else?)
  • XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)
  • css3 3D变换不能平滑地制作动画(css3 3D transform doesn't animate smoothly)
  • 运行时错误'91'和Outlook.Application = <对象变量或没有设置块变量>?(Run-time error '91' & Outlook.Application = Object variable or With block variable not set?)
  • 慢的webservice问题(Slow webservice problem)
  • textview的不正确对齐方式(Improper alignment of a textview)
  • 在第一步“Hello World”中出现Java错误(Cannot run “Hello World” program in Eclipse)
  • 为什么十六进制地址是14个字符?(why the hex address is 14 character?)
  • 如何在Python中的不同类中使用变量?(How to use variable in different classes in Python?)
  • asp:GridView HYPERLINKFIELD - datanavigateurlformatstring中的asp代码(asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring)
  • 关于adaboost算法(About adaboost algorithm)
  • 在Matlab上内置图像(Built in Images on Matlab)
  • java swing:输入键事件时焦点丢失(java swing: Focus lost on enter key event)
  • C#中的通用约束,T是相同的TSomethingElse,对吧?(Generic constraints in C#, T is the same TSomethingElse, right?)
  • 从mybatis中的光标获取数据(Fetching data from cursor in mybatis)
  • 在运行时从XML构建对象的最佳方法(Best way for building objects out of XMLs at runtime)
  • 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)
  • Selenium Node API Web驱动程序等待超时处理程序(Selenium Node API web driver wait timeout handler)
  • 函数使用并且是map的一部分(循环依赖?)(Function uses and is part of map (circular dependency?))
  • 是否可以在C ++ 14中使用可选模板参数创建类型元组?(Is possible to make a tuple of types with optional template parameters in C++14?)
  • PHP从窗帘后面打印/ f(PHP prints /f from behind the curtains)
  • JFrame的contentPane的LayoutManager(LayoutManager of JFrame's contentPane)
  • 用于Instagram Feed的图像滚动(Image Roll Overs for Instagram Feed)
  • 如何显示拉伸字体(双倍宽度/高度)?(How to display stretched font (double width/height)?)
  • 文件操作API在fileapi.h和stdio.h中的WP8差异?(WP8 differences between file manipulation APIs in fileapi.h and stdio.h?)
  • 保存失败后Rails没有回滚事务()(Rails not rolling back transaction after failed save())
  • jqgrid中的分页问题与数组数据(Pagination problem in jqgrid with array data)
  • 重定向时,通过其他页面上的URL调用javascript函数(Call javascript function through url on otherpage while Redirecting)
  • 如何使用InvokeCommandAction调用我的方法并传入参数?(How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?)
  • Jquerymobile按钮仅显示页面的第一个外观(Jquerymobile buttons are shown only first apperance of the page)