簡體   English   中英

如何使用在所有操作系統中兼容的FileInputStream讀取文件

[英]How to read a File using FileInputStream compatible in all OS

我有一個文件,我需要在Java中使用FileInputStream讀取它。 我需要提供所有操作系統都應可讀的路徑。 現在我給了

(new FileInputStream("..\\config.properties"));

這是Windows可讀的格式,但是在Unix中這是不可讀的。

是否有所有操作系統通用的方式。

您有兩種選擇:

  1. 對於獨立類,可以使用:

      new FileInputStream("../config.properties") 
  2. 對於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM