[英]How to read a File using FileInputStream compatible in all OS
I am having a file and i need to read it using FileInputStream in java. 我有一个文件,我需要在Java中使用FileInputStream读取它。 I need to give the path which should be readable in all OS.
我需要提供所有操作系统都应可读的路径。 Now i have given
现在我给了
(new FileInputStream("..\\config.properties"));
which is a windows readable format But this is a non readable in Unix. 这是Windows可读的格式,但是在Unix中这是不可读的。
Is there any way common for all OS. 是否有所有操作系统通用的方式。
You have two options: 您有两种选择:
For standalone classes you can use: 对于独立类,可以使用:
new FileInputStream("../config.properties")
For classes in JAR file you can use: 对于JAR文件中的类,可以使用:
InputStream input = getClass().getResourceAsStream("../config.properties");
This should help. 这应该有所帮助。
Yes. 是。 Instead of
代替
new FileInputStream("..\\config.properties")
This should work everywhere 这应该在任何地方都有效
new FileInputStream("../config.properties")
Or you could use 或者你可以使用
new FileInputStream(".." + java.io.File.separator + "config.properties") 新的FileInputStream(“ ..” + java.io.File.separator +“ config.properties”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.