![](/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.