Latest web development tutorials

Java File class

Java class files in an abstract way represents the file name and directory path name. This class is mainly used to create files and directories, delete files and find files.

File object represents the actual existence of the disk files and directories. Create a File object constructed by the following method.

String to create a new File instance given by the parent abstract pathname and a child pathname.

File(File parent, String child);

By the given pathname string into an abstract pathname to create a new File instance.

File(String pathname) 

String to create a new File instance from a parent pathname string and a child pathname.

File(String parent, String child) 

By the given file: URI into an abstract pathname to create a new File instance.

File(URI uri) 

After you create a File object, you can use the following method of operating the file list.

No. Method Description
1 public String getName ()
Returns the name of the file or directory thus abstract pathname that.
2 public String getParent (),
Returns the parent pathname of this abstract pathname pathname strings, if this pathname does not name a parent directory, it returns null .
3 public File getParentFile ()
Returns the parent abstract pathname pathname of this abstract pathname, if this pathname does not name a parent directory, it returns null .
4 public String getPath ()
Converts this abstract pathname into a pathname string.
5 public boolean isAbsolute ()
Test whether this abstract pathname is absolute pathname.
6 public String getAbsolutePath ()
Returns the absolute path name string abstract pathname.
7 public boolean canRead ()
Tests whether the application can read this abstract pathname of the file.
8 public boolean canWrite ()
Tests whether the application can modify this abstract pathname of the file represented.
9 public boolean exists ()
File or directory Tests this abstract pathname that exists.
10 public boolean isDirectory ()
Tests whether this abstract pathname that the file is a directory.
11 public boolean isFile ()
Tests whether this abstract pathname of the file is represented by a standard document.
12 public long lastModified ()
Returns this abstract pathname that the file was last modified.
13 public long length ()
Returns this abstract pathname that the length of the file.
14 public boolean createNewFile () throws IOException
If and only if there is no file with this abstract pathname's name, the atoms created this abstract a new, empty file pathname.
15 public boolean delete ()
Delete this abstract pathname that the file or directory.
16 public void deleteOnExit ()
When a virtual machine is terminated, to request removal of this abstract pathname of the file or directory indicated.
17 public String [] list ()
Returns this abstract pathname name of the directory files and directories represented by an array of strings composed.
18 public String [] list (FilenameFilter filter )
Returns an array of strings naming the files and directories contained in the directory of the composition, and this directory is by satisfying the specified filter abstract pathname represented.
19 public File [] listFiles ()
Returns an array of abstract pathnames, these path names represent files in the directory denoted by this abstract pathname.
20 public File [] listFiles (FileFilter filter )
Returns an array of abstract pathnames denoting the files in the directory and the directory denoted by this abstract pathname that satisfy the specified filter path name.
twenty one public boolean mkdir ()
Create this abstract pathname specified directory.
twenty two public boolean mkdirs ()
Create this abstract pathname specified directory, including any necessary but nonexistent parent directories.
twenty three public boolean renameTo (File dest)
Rename this abstract pathname of the file represented.
twenty four public boolean setLastModified (long time)
Setting this abstract pathname specified file or directory last modified.
25 public boolean setReadOnly ()
Tag this abstract pathname of the file or directory so that it can only be read.
26 public static File createTempFile (String prefix, String suffix, File directory) throws IOException
Create a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
27 public static File createTempFile (String prefix, String suffix) throws IOException
Create an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
28 public int compareTo (File pathname)
Alphabetically Compares two abstract pathnames.
29 public int compareTo (Object o)
Alphabetically abstract pathname with the given object.
30 public boolean equals (Object obj)
Tests this abstract pathname for equality with the given object.
31 public String toString ()
Returns this abstract pathname pathname string.

Examples

The following example demonstrates the use of File object:

import java.io.File;
public class DirList {
   public static void main(String args[]) {
      String dirname = "/java";
      File f1 = new File(dirname);
      if (f1.isDirectory()) {
         System.out.println( "Directory of " + dirname);
         String s[] = f1.list();
         for (int i=0; i < s.length; i++) {
            File f = new File(dirname + "/" + s[i]);
            if (f.isDirectory()) {
               System.out.println(s[i] + " is a directory");
            } else {
               System.out.println(s[i] + " is a file");
            }
         }
      } else {
         System.out.println(dirname + " is not a directory");
    }
  }
}

The above examples compiled results are as follows:

Directory of /mysql
bin is a directory
lib is a directory
demo is a directory
test.txt is a file
README is a file
index.html is a file
include is a directory