How to Add Directory Names to Your Java Filenames
You can specify a file’s exact location in your Java code. Code like
new File("C:\\Users\\bburd\\workspace\\08-01\\EmployeeInfo.txt") looks really ugly, but it works.
In the preceding paragraph, did you notice the double backslashes in “
C: \\Users\\bburd\\workspace …”? If you’re a Windows user, you’d be tempted to write
C:\Users\bburd\workspace … with single backslashes. But in Java, the single backslash has its own, special meaning. So, in Java, to indicate a backslash inside a quoted string, you use a double backslash instead.
Macintosh and Linux users might find comfort in the fact that their path separator,
/, has no special meaning in a Java string. On a Mac, the code
new File("/Users/bburd/workspace/08-01/EmployeeInfo.txt") is as normal as breathing. (Well, it’s almost that normal!) But Mac users and Linux wonks shouldn’t claim superiority too quickly.
Lines such as
new File("/Users/bburd/workspace … work in Windows as well. In Windows, you can use either a slash (
/) or a backslash (
\) as the path name separator. At the Windows command prompt, you can type
cd c:/users\bburd to get to your home directory.