Java NIO File操作

今天在工作中遇到一个很奇怪的问题。使用java.io.File进行文件操作抛出了FileNotFoundException,代码如下

//但事实上在`/somewhere`目录下存在文件`测试.txt`
FileInputStream fin = new FileInputStream(new File("/somewhere/测试.txt"));

在网络找了下,发现一个可能是由于路径中包括非ASCII字符,详见这里

最后找到了解决方案

1

InputStream fin = Files.newInputStream(Paths.get("/somewhere/测试.txt"));

看来Java老的那一套File操作接口确实问题多多啊。这里将Java 7引入的新File操作接口复习一下以备忘。

Path接口

这个接口表示一个文件在文件系统中的定位器。常见与文件路径相关的操作都可以在这个接口里找到。详细文档参见这里

注意以下两点 * Path对象一般由java.nio.file.Paths的两个get静态方法得来,并不是new出来的 * 如果与已有的库交互要用到java.io.File,可以使用它的toFile方法得到一个File对象

Files接口

Files接口里基本上都是静态方法,所有与文件相关的操作都可以在这个接口里找到。详细文档参见这里

为了便于更好地使用Files进行文件操作,这里列举经常用到的静态方法,并与使用java.io.File作个参照。

  • Files.exists(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).exists()
  • Files.newInputStream(Paths.get("/somewhere/somefile.txt")) vs new FileInputStream(new File("/somewhere/somefile.txt"))
  • Files.newOutputStream(Paths.get("/somewhere/somefile.txt")) vs new FileOutputStream(new File("/somewhere/somefile.txt"))
  • Files.newByteChannel(Paths.get("/somewhere/somefile.txt")) vs (new FileInputStream(new File("xxx")).getChannel()
  • Files.newDirectoryStream(Paths.get("/somewhere")) vs (new File("/somewhere")).listFiles()
  • Files.createFile(Paths.get("/somewhere/somefile.txt") vs (new File("/somewhere/somefile.txt")).createNewFile()
  • Files.createDirectory(Paths.get("/somewhere")) vs (new File("/somewhere")).mkdir()
  • Files.createDirectories(Paths.get("/somewhere")) vs (new File("/somewhere")).mkdirs()
  • Files.createAndCheckIsDirectory(Paths.get("/somewhere")) vs (new File("/somewhere")).mkdirs(); boolean success = (new File("/somewhere")).exists() && (new File("/somewhere")).isDirectory()
  • Files.createTempFile(Paths.get("/somewhere"), "XXXX", null) vs File.createTempFile("XXXX", null, new File("/somewhere"))
  • Files.createTempFile("XXXX", null) vs File.createTempFile("XXXX", null)
  • Files.createTempDirectory(Paths.get("/somewhere"), "XXXX") vs -
  • Files.createTempDirectory("XXXX") vs -
  • Files.createSymbolicLink(Paths.get("/somewhere/link.txt"), Paths.get("/somewhere/somefile.txt")) vs -
  • Files.createLink(Paths.get("/somewhere/link.txt"), Paths.get("/somewhere/somefile.txt")) vs -
  • Files.delete(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).delete()
  • Files.deleteIfExists(Paths.get("/somewhere/somefile.txt")) vs -
  • Files.copy(Paths.get("/somewhere/somefile.txt"), Paths.get("/somewhere/somefile2.txt")) vs -
  • Files.move(Paths.get("/somewhere/somefile.txt"), Paths.get("/somewhere/somefile2.txt")) vs (new File("/somewhere/somefile.txt")).renameTo(new File("/somewhere/somefile2.txt"))
  • Files.readSymbolicLink(Paths.get("/somewhere/link.txt")) | -
  • Files.isSameFile(Paths.get("/somewhere/somefile.txt"), Paths.get("/somewhere/somefile2.txt")) | -
  • Files.isHidden(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).isHidden()
  • Files.getFileAttributeView(Paths.get("/somewhere/somefile.txt"), AclFileAttributeView.class) vs -
  • Files.readAttributes(Paths.get("/somewhere/somefile.txt"), PosixFileAttributes.class) vs -
  • Files.setAttribute(Paths.get("/somewhere/somefile.txt"), "dos:hidden", true) vs -
  • Files.getAttribute(Paths.get("/somewhere/somefile.txt"), "unix:uid") | -
  • Files.readAttributes(Paths.get("/somewhere/somefile.txt"), "posix:permissions,owner,size") vs -
  • Files.getPosixFilePermissions(Paths.get("/somewhere/somefile.txt")) vs -
  • Files.setPosixFilePermissions(Paths.get("/somewhere/somefile.txt"), new HashSet(){ {this.add(PosixFilePermission.OWNER_READ);this.add(PosixFilePermission.OWNER_WRITE);this.add(PosixFilePermission.OWNER_EXECUTE);} }) vs -
  • Files.getOwner(Paths.get("/somewhere/somefile.txt")) vs -
  • UserPrincipal joe = lookupService.lookupPrincipalByName("joe");Files.setOwner(Paths.get("/somewhere/somefile.txt"), joe) vs -
  • Files.isSymbolicLink(Paths.get("/somewhere/somefile.txt")) vs -
  • Files.isDirectory(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).isDirectory()
  • Files.isRegularFile(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).isFile()
  • Files.getLastModifiedTime(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).lastModified()
  • Files.setLastModifiedTime(Paths.get("/somewhere/somefile.txt"), FileTime.fromMillis(System.currentTimeMillis())) vs (new File("/somewhere/somefile.txt")).setLastModified(System.currentTimeMillis())
  • Files.size(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).length()
  • Files.notExists(Paths.get("/somewhere/somefile.txt")) vs !(new File("/somewhere/somefile.txt")).exists()
  • Files.isAccessible(Paths.get("/somewhere/somefile.txt")) vs -
  • Files.isReadable(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).canRead()
  • Files.isWritable(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).canWrite()
  • Files.isExecutable(Paths.get("/somewhere/somefile.txt")) vs (new File("/somewhere/somefile.txt")).canExecute()
  • Files.walkFileTree(Paths.get("/somewhere"), new FileVisitor<Path>(){…}) vs -
  • Files.newBufferedReader(Paths.get("/somewhere/somefile.txt")) vs new InputStreamReader(new FileInputStream(new File("/somewhere/somefile.txt")))
  • Files.newBufferedWriter(Paths.get("/somewhere/somefile.txt")) vs new OutputStreamWriter(new FileOutputStream(new File("/somewhere/somefile.txt")))
  • Files.list(Paths.get("/somewhere")) vs (new File("/somewhere")).listFiles()

有一些Stream与Path相互copy、一些重载的方法没有列在上面。

官方为什么又发明一个新的文件操作API呢?我想了下,感觉大概是以下原因

  • 静态方法比对象方法更易用
  • Files的静态方法实现得更跨平台
  • 对于文件属和权限的操作更方便易用
  • Files的静态方法在文件操作出错时能更好地给予外界信息供诊断
  • Files的静态方法处理比较大的目录时,因为使用了Visitor模式,比老的方式效率更高
  • Files的move方法操作更原子性
  • 老的API已被广泛使用,不好直接对其作大幅修改

另外原来的deleteOnExit建议换成下面的写法

OutputStream out = Files.newOutputStream(Paths.get("test.tmp"), StandardOpenOption.DELETE_ON_CLOSE);
...
out.close();

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏BinarySec

CVE-2010-3333分析[漏洞战争]

CVE-2010-3333漏洞是一个栈溢出漏洞,该漏洞是由于Microsoft word在处理RTF数据的对数据解析处理错误,可被利用破坏内存,导致任意代码执行...

47340
来自专栏我和未来有约会

silverlight 《Hands-On-Labs》教程系列

Silverlight Fundamentals: Basic concepts of Silverlight 2 development [开发的基础] T...

20970
来自专栏张善友的专栏

CentOS 7 上面安装PowerShell

看了文章 爱上PowerShell , 就想在CentOS 7上面试试PowerShell , 本文记录了在CentOS 7.2上安装Powershell 的过...

27880
来自专栏landv

c语言-error C2440: “static_cast”: 无法从“UINT (__thiscall CHyperLink::* )(CPoint)”转换为“LRESULT (__thiscall

20630
来自专栏跟着阿笨一起玩NET

C# 子窗体中调用父窗体中的方法(或多窗体之间方法调用)

本文转载:http://www.csframework.com/archive/2/arc-2-20110805-1771.htm

1.5K20
来自专栏跟着阿笨一起玩NET

ASP.NET WEB API 调试

RouteDebugger 是调试 ASP.NET MVC 路由的一个好的工具,在ASP.NET WEB API中相应的有 WebApiRouteDebugge...

38220
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

496100
来自专栏跟着阿笨一起玩NET

C#/WinForm给控件加入hint文字

以前在手机上(wp)上做过类似的效果。参考silverlight toolkit 的searchTextBox。现在要在winform下制作,开始我还以为应该有...

21100
来自专栏跟着阿笨一起玩NET

关于Form窗体的StartPosition 属性如何设置的问题

故只有设置StartPosition 属性放在窗体构造函数中;而不能放在Load事件中。

16520
来自专栏.NET开发者社区

​(码友推荐)2018-09-18 .NET及相关开发资讯速递

1.Redis 桌面管理工具 RedisDesktopManager 0.9.6 发布

13750

扫码关注云+社区

领取腾讯云代金券