M.M.RAM KUMAR
M.M.RAM KUMAR

Reputation: 115

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.

Upvotes: 1

Views: 148

Answers (2)

aryann
aryann

Reputation: 949

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.

Upvotes: 4

Elliott Frisch
Elliott Frisch

Reputation: 201527

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

Upvotes: 2

Related Questions