简体   繁体   中英

Given a pathname, how can I tell whether the object is a directory or a file using the Java File API?

Given the absolute path as a variable String pathname, how can I do the following using the Java Class File? The answers as far as I know are in parenthesis, please confirm.

  1. get the size of the file/directory? (I'm not sure)

  2. test if the absolute path even leads to something that exists? (File.exists(pathname), which should return a boolean true if it exists, boolean false otherwise)

  3. see if this is this a file or directory? (File.isFile(pathname), which returns a boolean true if the pathname leads to a file, false otherwise. file.isDirectory(pathname), which returns a boolean true if the pathname leads to a directory, false otherwise)

  4. see the last date modified? (File.lastModified(pathname), which returns a long number which I already have the method for to convert to a specific date

A related but separate question: is there something that is a "file", like a .doc, .jpeg, .mpeg, .mp3, .xml, .* that will fail both isFile and isDirectory tests? If so, how can I distinguish between an empty folder and a folder with a file that fails both isFile and isDirectory tests? I'm asking because on the File class documentation, it states the following for isFile: Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

File f = new File(path);
// Get size of file (not a directory though)
f.length();
// Check if file exists
f.exists();
// Check if file is directory
f.isDirectory();
// Check if file is file
f.isFile();
// Last modified date
f.lastModified();

Reference: JavaDoc for java.io.File

If you want the size of a directory, you'd have to combine these methods while you traverse the directory you want to compute the size for, or you could use a third party library like FileUtils in Apache Commons IO which has a sizeOf method that apparently handles size of directories as well.

  • Test if exists: file.exists()
  • Test if a normal file: file.isFike()
  • Something that us neither a "file" not a directory: A symbolic link

Is there something that is a "file", like a .doc, .jpeg, .mpeg, .mp3, .xml, .* that will fail both isFile and isDirectory tests?

No. Those are all regular files. If it is a file, it will pass the isFile test.

The things that are not "normal files" or directories are things like device files and named pipe files. The java.io.File cannot accurately distinguish these ... or symbolic links.

If you want to be able to do operating system specific things, take a look at Java 7's java.nio.file package and specifically the Files class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM