Home Java Example Java Io Java IO File

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO File

Advertisement
In this section we will discuss about the File class in Java.

Java IO File

In this section we will discuss about the File class in Java.

A Computer File is used to store the data. These data can be String, characters, numbers etc and may arranged in string, rows, columns, lines etc. In Java we can also create a File to store the data. To create file in Java a class named File is provided in java.io package. Using constructors and methods of this class a Java programmer can create a new file. File.createNewFile() method creates the new file. java.io.File creates the new file with the abstract directory path names. An O/S and user interfaces point of view file and directories names are named as system dependent pathname strings. java.io.File introduced an abstract, system independent file and directory pathnames. This pathname contains the following two components :

  • System dependent prefix (optional) i.e. ' / ' used for specifying the root directory in UNIX or, ' \\ ' used for specifying the root directory in Windows and
  • zero or more string names.

Constructor Detail

Constructor
Description
File(File parent, String child) This constructor is used to create a new File object with the parent path name and child pathname.
Syntax : public File(File parent, String child)
File(String pathname) This constructor is used to create a new File object with specified file pathname.
Syntax : public File(String pathname)
File(String parent, String child) This constructor is used to create a new File object with the specified parent pathname and child pathname.
Syntax : public File(String parent, String child)
File(URI uri) This constructor is used to create a new File object by converting a specified URI into pathname.
Syntax : public File(URI uri)

Method Detail

java.io.File has various methods some commonly used methods are as follows :

  • canExecute() : This method is used for testing that whether the file can be executed by the application or not.

    Syntax : public boolean canExecute()
     
  • canRead() : This method is used for testing that whether the file can be read by the application or not.

    Syntax : public boolean canRead()
     
  • canWrite() : This method is used for testing that whether the file can be write by the application or not.

    Syntax : public boolean canWrite()
     
  • compareTo(File pathname) : This method is used to compare two file pathname.

    Syntax : public int compareTo(File pathname)
     
  • createNewFile() : This method is used for creating a new empty file with the specified name.

    Syntax : public boolean createNewFile() throws IOException
     
  • createTempFile(String prefix, String suffix) : This method is used for creating an empty file in the default temporary file directory. prefix and suffix are used for specifying the name of file.

    Syntax : public static File createTempFile(String prefix,String suffix) throws IOException
     
  • createTempFile(String prefix, String suffix, File directory) : This method is used for creating an empty file in the given directory. prefix and suffix are used for specifying the name of file.

    Syntax : public static File createTempFile(String prefix, String suffix, File directory) throws IOException
     
  • delete() : This method is used for deleting the specified file or directory.

    Syntax : public boolean delete()
     
  • deleteOnExit() : This method is used for deleting the specified file or directory on the termination of JVM.

    Syntax : public void deleteOnExit()
     
  • equals(Object obj) : This method is used to test whether the specified Object is equal to the pathname or not.

    Syntax : public boolean equals(Object obj)
     
  • exists() : This method is used to test whether the specified pathname is existed or not.

    Syntax : public boolean exists()
     
  • getAbsoluteFile() : This method is used to get the absolute form of pathname.

    Syntax : public File getAbsoluteFile()
     
  • getAbsolutePath() : This method is used to get the absolute pathname. This pathname is retrieved as string.

    Syntax : public String getAbsolutePath()
     
  • getFreeSpace() : This method is used to find out the unallocated bytes in the partition (portion of storage for a file system.)

    Syntax : public long getFreeSpace()
     
  • getName() : This method is used to find out the name of file or directory of the specified pathname.

    Syntax : public String getName()
     
  • getParent() : This method is used to find out the parent of the specified pathname.

    Syntax : public String getParent()
     
  • getParentFile() : This method is used to find out the parent of the specified pathname.

    Syntax : public File getParentFile()
     
  • getPath() : This method is used to find out the path of the file or directory as string.

    Syntax : public String getPath()
     
  • getTotalSpace() : This method is used to find out the size of the partition.

    Syntax : public long getTotalSpace()
     
  • getUsableSpace() : This method is used to find out the number of bytes made available to the VM when the partition named by the pathname.

    Syntax : public long getUsableSpace()
     
  • hashCode() : This method is used for computing a hash code for pathname.

    Syntax : public int hashCode()
     
  • isAbsolute() : This method is used for testing that whether the pathname is absolute or not.

    Syntax : public boolean isAbsolute()
     
  • isDirectory() : This method is used for testing that whether the specified pathname is directory or not.

    Syntax : public boolean isDirectory()
     
  • isFile() : This method specifies whether the specified file name is a normal file or not.

    Syntax : public boolean isFile()
     
  • isHidden() : This method specifies, whether the file is a hidden file or not.

    Syntax : public boolean isHidden()
     
  • lastModified() : This method is used to get the time in which the file was last modified.

    Syntax : public long lastModified()
     
  • length() : This method is used to get the length of the file.

    Syntax : public long length()
     
  • list() : This method is used to get the list of files and directories (as an array of strings) in the specified directory

    Syntax : public String[] list()
     
  • list(FilenameFilter filter) : This method is used to get the list of files and directories (as an array of strings) in the specified directory which specifies a specified filter.

    Syntax : public String[] list(FilenameFilter filter)
     
  • listFiles() : This method is used to get the (an array) pathnames which represents files in the directory.

    Syntax : public File[] listFiles()
     
  • listFiles(FileFilter filter) : This method is used to get the (an array) pathnames which represents files and directories in the specified directory which specifies a specified filter.

    Syntax : public File[] listFiles(FileFilter filter)
     
  • listFiles(FilenameFilter filter) : This method is used to get the (an array) pathnames which represents files and directories in the specified directory which specifies a specified filter.

    Syntax : public File[] listFiles(FilenameFilter filter)
     
  • listRoots() : This method is used to list the file system roots (if available).

    Syntax : public static File[] listRoots()
     
  • mkdir() : This method is used to create a directory with the specified pathname.

    Syntax : public boolean mkdir()
     
  • mkdirs() : This method is used to create a directory with the specified pathname.

    Syntax : public boolean mkdirs()
     
  • renameTo(File dest) : This method is used to rename the file specified by the pathname.

    Syntax : public boolean renameTo(File dest)
     
  • setExecutable(boolean executable) : This method is used to set the permission of execution of files.

    Syntax : public boolean setExecutable(boolean executable)
     
  • setExecutable(boolean executable, boolean ownerOnly) : This method is used to set the permission of execution of files.

    Syntax : public boolean setExecutable(boolean executable, boolean ownerOnly)
     
  • setLastModified(long time) : This method is used to set the file's or directory's last modified time.

    Syntax : public boolean setLastModified(long time)
     
  • setReadable(boolean readable) : This method is used to set the permission of file to read.

    Syntax : public boolean setReadable(boolean readable)
     
  • setReadable(boolean readable, boolean ownerOnly) : This method is used to set the permission of file to read.

    Syntax : public boolean setReadable(boolean readable, boolean ownerOnly)
     
  • setReadOnly() : This method is used to set the file's or directory's to allowed only for read operation.

    Syntax : public boolean setReadOnly()
     
  • setWritable(boolean writable) : This method is used to set the file's or directory's to allowed only for write operation

    Syntax : public boolean setWritable(boolean writable)
     
  • setWritable(boolean writable, boolean ownerOnly) : This method is used to set the file's or directory's to allowed only for write operation.

    Syntax : public boolean setWritable(boolean writable, boolean ownerOnly)
     
  • toString() : This method is used to get the pathname of the specified abstract pathname as string.

    Syntax : public String toString()
     

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 26, 2012

Related Tutorials

Discuss: Java IO File  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com