简体   繁体   中英

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. 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.

Is there any way common for all OS.

You have two options:

  1. For standalone classes you can use:

      new FileInputStream("../config.properties") 
  2. For classes in JAR file you can use:

     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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM