Home

How to Add Directory Names to Your Java Filenames

Updated
2017-05-02 12:51:53
From the book
Share
Java Essentials For Dummies
Cover of Java Essentials for Dummies book by Doug Lowe and Paul McFedries with key learning points.
Explore Book
Buy NowSubscribe on Perlego
Java Essentials For Dummies
Cover of Java Essentials for Dummies book by Doug Lowe and Paul McFedries with key learning points.Explore Book
Buy NowSubscribe on Perlego
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:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.