Java.io - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io File

Java.io File

shape Introduction

The Java.io File is started to use in java 1.0 onwards but coming to 1.2, 1.3 slowly stopped using Java.io File classes and used databases like oracle mysql to store and access data. But from java1.5 onwards Java.io File is also using because of storing huge data we are going for database then to store bits of data use file i/o.

shape Syntax

 File f = newfile (“a.text”); Because of the above syntax, no physical file is created. Only the java file object with name a.text is created. The function of  this object is to check whether the file is already existing or not. If file is available then f pointing to that file else creating an new file by using below syntax. The java file object represents both directory and file.

shape Syntax

F.create newfile();

Class Declaration

shape Declaration

Java.io.File class is declared as shown below: Public class File extends Object implements serializable

shape Fields

The fields for Java.io File class are as follows:
  • Static String separator: The String separator field depends on system and represents with name string by default.
  • Static String pathSeparator: The String pathSeparator field depends on system and represents the pathSeparator as string.
  • Static char separatorChar: The Char SeparatorChar field depends on system and represents with name Char by default.
  • Static char pathSeparatorChar: The Char pathSeparatorChar field depends on system and represents with name Char.

Class Constructors

shape Table

Constructor Description
File(File parent, String child) With respect to both parent and child abstract pathname creating a new file object.
File(String pathname) By changing the string into parent abstract pathname creating a new file object using this method.
File(URI Uri) By converting available URI file into  abstract pathname for to create new file object.
File(String parent, String child) By making use of parent string and child string new file instance is creating in this method.

Class Methods

shape Table

Method Description
boolean canExecute() By its abstract path name the program execute the file is tested with the current method and returns Boolean value.
boolean canRead() By its abstract path name the program reads the file is tested with the current method and returns value in boolean.
boolean canWrite() By its abstract path name the program writes the file is tested with this current method and returns value in boolean.
boolean createNewFile() By its abstract path name the method creates a new file which doesn’t exist already with the same name.
boolean delete() The files or directories which are denoted by its abstract path name are deleted.
boolean exists() By its abstract path name the method checks whether the directory or file  either exist or not.
boolean equals(Object obj) To get equality for the given object this method checks its abstract path name.
boolean isAbsolute() The abstract path name is tested by this method whether it is absolute or not .
boolean isDirectory() The file tested by the current method represents directory denoted with abstract path name.
boolean isFile() The file tested by the current method represents normal file denoted with abstract path name.
boolean isHidden() The file which represents by its abstract path name is hidden file, tested by the current method.
boolean mkdir() The new directory is created with the current method.
boolean mkdirs() The new directory is created with the current method but not exist parent directories.
boolean renameTo(File dest) The use of this method is to rename the file.
boolean setExecutable(boolean executable) To this abstract path name the convenience method sets the owner’s to execute permission.
boolean setExecutable(boolean executable, boolean ownerOnly) To this abstract path name the method sets the owner’s to execute permission.
boolean setLastModified(long time) To this abstract path name the method can set the last modified time of the  directory or file .
boolean setReadable(boolean readable) To this abstract path name the method sets the owner’s to read permission.
boolean setReadable(boolean readable, boolean ownerOnly) To this abstract path name the method sets the owners to read permission.
boolean setReadOnly() This method allows the read operations only from the file or directory.
boolean setWritable(boolean writable) To this abstract path name the method can set owner’s to write the permission.
boolean setWritable(boolean writable, boolean ownerOnly) To this abstract path name the method can set the owner’s only to write the permission.
String toString() To abstract path name the method returns the pathname string.
URI toURI() The URI file which is constructed by this method represents abstract pathname.
int compareTo(File pathname) Abstract pathnames are compared by this method lexicographically.
void deleteOnExit() At the time when the JVM terminates the directories  or files which are denoted by its abstract pathname are deleted.
static File createTempFile(String prefix, String suffix) By making use of the specified prefix and suffix to generate name of the empty file creates by this method in default temporary file directory.
static File createTempFile(String prefix, String suffix, File directory) By making use of the specified prefix and suffix to generate name of the empty file creates by this method in specified file directory.
String getAbsolutePath() By its abstract pathname the method returns absolute pathname.
File getAbsoluteFile() By its abstract pathname the method returns absolute file.
File getCanonicalFile() By its abstract pathname the method returns canonical form.
String getCanonicalPath() By its abstract pathname the method returns canonical pathname.
long getFreeSpace() By its abstract pathname the method returns bytes which are unallocated in the partition.
String getName() The files or directories which are denoted by its abstract pathname are named by this method.
String getParent() In this method the parent path name returns abstract path name of its string. If parent directory doesn't name it returns to be null.
String getPath() The path name string is obtained from abstract path name by this method.
File getParentFile() In this method the parent path name returns abstract path name of its string. If parent directory doesn't name it returns to be null.
long getTotalSpace() By its abstract path name the method returns  size of the named partition.
long getUsableSpace() By its abstract path name the method returns to the JVM that number of bytes exists already.
int hashCode() For its abstract pathname this method computes a hash code.
long lastModified() The file which is represented by abstract path name is  modified at last, which was return by this method.
long length() By its abstract path name the method returns the file length.
String[] list() By its abstract path name the method returns the strings representing the files or directories in the form of an array.
String[] list(FilenameFilter filter) By its abstract path name, method returns strings representing files and directory in the form of array which satisfies the represented filter.
File[] listFiles() By its abstract path name, method returns the files in the directory in the form of array.
File[] listFiles(FileFilter filter) By its abstract path name, method returns the files in the directory in the form of array which satisfies the represented filter.
File[] listFiles(FilenameFilter filter) By its abstract path name, method returns the files in the directory in the form of array which satisfies the represented filter.
static File[] listRoots() This method lists about existing file system roots.

Inherited Methods

shape Description

From the following class, methods are inherited to Java.io.File :
  • Java.io.Object

shape Examples

Usage of  boolean canExecute() method. [c]import java.io.File; public class FileDemo { public static void main(String[] args) { File fl = null; String[] str = { "test.txt", "/test.txt" }; try { // Representing each string in string array for (String s : str) { // Creating new file fl = new File(s); // file is executable then it returns true boolean bool = fl.canExecute(); // To find the absolute path String a = fl.getAbsolutePath(); // To prints absolute path System.out.print(a); // print System.out.println(" is executable: " + bool); } } catch (Exception e) { // if any issues I/O error occurs e.printStackTrace(); } } }[/c] OutPut The result is as follows. [c]D:\Spring\Test\test.txt is executable: false D:\test.txt is executable: false[/c] Usage of Boolean canRead() method. [c] import java.io.File; public class Splesson { public static void main(String[] args) { File f = null; try { // creating a new file f = new File("c:/test.txt"); // if the file can be read it returns true boolean bl = f.canRead(); // print System.out.print("File can be read: " + bl); } catch (Exception e) { // if any issues I/O error occurs e.printStackTrace(); } } }[/c] OutPut The result is as follows. [c] File can be read: false[/c] Usage of Boolean mkdir() method. [c]import java.io.File; public class FileMkdir { //public class FileDemo { public static void main(String[] args) { File f = null; boolean bl = false; try{ //For files and directory it returns pathnames f = new File("C:/Text"); // create a directory bl = f.mkdir(); // print the output System.out.print("Directory created? "+bl); }catch(Exception e) { // if any other error occurs e.printStackTrace(); } } }[/c] OutPut The result is as follows. [c]Directory created? true [/c] Usage of File.Length() method. [c]import java.io.File; public class FileLength { //public class FileDemo { public static void main(String[] args) { File f = null; String path; long n; boolean bl = false; try{ // creating new file f = new File("c:/abc.txt"); // Returns true if the file path is a file, else false bl = f.exists(); // If the path exists if(bl) { // It returns the length in bytes n = f.length(); // Represents path path = f.getPath(); // printing the length of file System.out.print(path+" file length: "+n); } }catch(Exception e){ // if any other error occurs e.printStackTrace(); } } } [/c] OutPut The result is as follows. [c]c:\abc.txt file length: 5 [/c] Usage of File.isHidden() method. [c]import java.io.File; public class FileHidden { public static void main(String[] args) { File f = null; String path; boolean bl = false; try{ // creating new file f = new File("c:/abc.txt"); // Returns true if the file path is a hidden file bl = f.isHidden(); // get the file path path = f.getPath(); // prints is file hidden or not System.out.print(path+" is file hidden? "+bl); }catch(Exception e) { // if any error occurs e.printStackTrace(); } } }[/c] OutPut The result is as follows. [c]c:\abc.txt is file hidden? false[/c]

Summary

shape Key Points

  • Java.io File - The Java.io File is started to use in java 1.0 onwards.
  • Java.io File - But from java1.5 onwards Java.io File is also using because of storing huge data we are going for database then to store bits of data use file i/o.