File path \\ //

Robertovich Wartins
Greenhorn
Posts: 16
How correctly i should open file?
FileInputStream fInStream = new FileInputStream("C:\\myExample.txt");
or
FileInputStream fInStream = new FileInputStream("C://myExample.txt");
should i use // or \\ what is the difference?

Peter Chase
Ranch Hand
Posts: 1970
Backslash is the correct file separator for Windows, but forward slash is accepted in many situations. Doubling the backslash is necessary because backslash is an escape character in Java string literals. Doubling the forward slash is just wrong, and I am surprised if it works.

Note that hard-coding such a path makes your code specific to Windows, and specific to a particular drive set-up. That's not good practice for anything except a quick and dirty throw-away application.

To make code more re-usable, paths can be passed in on the command line, via Java system properties, properties files, Java configuration API, or even system environment variables. When constructing paths, use File.separator, rather than hard-coding forward slash, backslash or any other separator (e.g. Macs sometimes have colons as separators).
[ January 31, 2007: Message edited by: Peter Chase ]

Robertovich Wartins
Greenhorn
Posts: 16
thank you - again i learned something