Java Reference
In-Depth Information
49
System.out.printf( "%s does not exist%n" , path);
50
}
51
} // end main
52
} // end class FileAndDirectoryInfo
Enter file or directory name:
c:\examples\ch15
ch15 exists
Is a directory
Is an absolute path
Last modified: 2013-11-08T19:50:00.838256Z
Size: 4096
Path: c:\examples\ch15
Absolute path: c:\examples\ch15
Directory contents:
C:\examples\ch15\fig15_02
C:\examples\ch15\fig15_12_13
C:\examples\ch15\SerializationApps
C:\examples\ch15\TextFileApps
Enter file or directory name:
C:\examples\ch15\fig15_02\FileAndDirectoryInfo.java
FileAndDirectoryInfo.java exists
Is not a directory
Is an absolute path
Last modified: 2013-11-08T19:59:01.848255Z
Size: 2952
Path: C:\examples\ch15\fig15_02\FileAndDirectoryInfo.java
Absolute path: C:\examples\ch15\fig15_02\FileAndDirectoryInfo.java
Fig. 15.2 | File class used to obtain file and directory information . (Part 2 of 2.)
Error-Prevention Tip 15.1
Once you've confirmed that a Path exists, it's still possible that the methods demonstrated in
Fig. 15.2 will throw IOException s. For example, the file or directory represented by the
Path could be deleted from the system after the call to Files method exists and before the
other statements in lines 24-45 execute. Industrial strength file- and directory-processing
programs require extensive exception handling to recover from such possibilities.
Separator Characters
A separator character is used to separate directories and files in a path. On a Windows
computer, the separator character is a backslash ( \ ). On a Linux or Mac OS X system, it's
a forward slash ( / ). Java processes both characters identically in a path name. For example,
if we were to use the path
c:\Program Files\Java\jdk1.6.0_11\demo/jfc
which employs each separator character, Java would still process the path properly.
 
Search WWH ::




Custom Search