Java For Dummies, 8th Edition
Book image
Explore Book Buy On Amazon
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.

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a veteran author and educator. At the University of Illinois, he was named five times to the university-wide List of Teachers Ranked as Excellent by their Students. He has written several books on Java and Android development.

This article can be found in the category: