Ronaldinho Learn Coding
Ronaldinho Learn Coding

Reputation: 13824

FileInputStream read file from specific location?

If I place text file in same project folder, program can read it without problems. But how can I make it read a file from somewhere in my computer (ex: in Desktop)

FileInputStream fstream = new FileInputStream("Contact.txt");

I change it to "C:...\Desktop\Contact.txt") but I getting error.

Upvotes: 0

Views: 3879

Answers (2)

travega
travega

Reputation: 8415

You should use File.separator instead of / and \\ to delimit your path as this is will work on both Window and Unix based systems. This might help to convince you...!

Upvotes: 2

Yogendra Singh
Yogendra Singh

Reputation: 34367

Change the backslash (\) to forwardslash(/) or use double backslashes (\\) in your file path.

  FileInputStream fstream = new FileInputStream("C:.../Desktop/Contact.txt");

or

  FileInputStream fstream = new FileInputStream("C:...\\Desktop\\Contact.txt");

Please Note: \ is an escape character. If you use \\, it will use single \ as literal.

Upvotes: 4

Related Questions