Home

How to Add Directory Names to Your Java Filenames

|
Updated:  
2017-05-02 12:51:53
|
From The Book:  
Java Essentials For Dummies
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: