File.separator vs FileSystem.getSeparator()vs System.getProperty(" file.separator")? [英] File.separator vs FileSystem.getSeparator() vs System.getProperty("file.separator")?

查看:975
本文介绍了File.separator vs FileSystem.getSeparator()vs System.getProperty(" file.separator")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有三种相同的方法可以独立获得与平台相关的文件分隔符平台:

There seems to be three identical ways to get the platform-dependent "file separator" platform-independently:

java.nio.file.FileSystems.getDefault().getSeparator();

System.getProperty(file.separator)

我们如何决定何时使用哪个?

How do we decide when to use which?

它们之间是否有任何区别?

Is there even any difference between them?

推荐答案

System.getProperties()可以通过调用 System.setProperty来覆盖(字符串键,字符串值)或使用命令行参数 -Dfile.separator = /

System.getProperties() can be overridden by calls to System.setProperty(String key, String value) or with command line parameters -Dfile.separator=/

File.separator 获取默认文件系统的分隔符。

File.separator gets the separator for the default filesystem.

FileSystems.getDefault()获取默认文件系统。

FileSystem.getSeparator()获取文件系统的分隔符。请注意,作为一种实例方法,您可以使用此方法将不同的文件系统传递给除默认值之外的代码,前提是您需要代码在一个JVM中的多个文件系统上运行。

FileSystem.getSeparator() gets you the separator character for the filesystem. Note that as an instance method you can use this to pass different filesystems to your code other than the default, in cases where you need your code to operate on multiple filesystems in the one JVM.

这篇关于File.separator vs FileSystem.getSeparator()vs System.getProperty(" file.separator")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆