如何使用兼容所有操作系统的FileInputStream读取文件

时间:2013-12-10 04:11:48

标签: java fileinputstream

我有一个文件,我需要在java中使用FileInputStream来读取它。 我需要提供在所有操作系统中都应该可读的路径。 现在我已经给了

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

这是一种Windows可读格式 但这在Unix中是不可读的。

对于所有操作系统是否有任何共同点。

2 个答案:

答案 0 :(得分:4)

您有两种选择:

  1. 对于独立课程,您可以使用:

     new FileInputStream("../config.properties")
    
  2. 对于JAR文件中的类,您可以使用:

    InputStream input = getClass().getResourceAsStream("../config.properties");
    
  3. 这应该有所帮助。

答案 1 :(得分:2)

是。而不是

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

这应该适用于所有地方

new FileInputStream("../config.properties")

或者你可以使用

new FileInputStream(“..”+ java.io.File.separator +“config.properties”)