0

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.

Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110

2 Answers2

4

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.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
2

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

Community
  • 1
  • 1
travega
  • 8,284
  • 16
  • 63
  • 91