![](/img/trans.png)
[英]Java Inventory - How to read a file using FileInputStream?
[英]How to read a File using FileInputStream compatible in all OS
我有一个文件,我需要在Java中使用FileInputStream读取它。 我需要提供所有操作系统都应可读的路径。 现在我给了
(new FileInputStream("..\\config.properties"));
这是Windows可读的格式,但是在Unix中这是不可读的。
是否有所有操作系统通用的方式。
您有两种选择:
对于独立类,可以使用:
new FileInputStream("../config.properties")
对于JAR文件中的类,可以使用:
InputStream input = getClass().getResourceAsStream("../config.properties");
这应该有所帮助。
是。 代替
new FileInputStream("..\\config.properties")
这应该在任何地方都有效
new FileInputStream("../config.properties")
或者你可以使用
新的FileInputStream(“ ..” + java.io.File.separator +“ config.properties”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.