1

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.

M.M.RAM KUMAR
  • 115
  • 1
  • 2
  • 10
  • i don't know if this would help or not http://stackoverflow.com/questions/3548775/platform-independent-paths-in-java – insomniac Dec 10 '13 at 04:16
  • Refer to these already answered questions. http://stackoverflow.com/questions/17092260/fileinputstream-how-to-take-a-path-directly-without-adding-escape-characters http://stackoverflow.com/questions/9953328/weird-exception-in-thread-main-java-io-filenotfoundexception-i-o-java – user1933888 Dec 10 '13 at 04:17

2 Answers2

4

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.

aryann
  • 909
  • 5
  • 11
2

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

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249