Category: 09. Java.io package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTu7QxNc2xSxxckK_X5MKxqPL6enQiDOyfZ2w&s

  • FilterReader 

    Introduction

    The Java.io.FilterReader class is for reading filtered character streams. Following are the important points about FilterReader −

    • The class itself provides default methods that pass all requests to the contained stream.
    • The Subclasses of FilterReader should override some of these methods and may also provide additional methods and fields.

    Class declaration

    Following is the declaration for Java.io.FilterReader class −

    public abstract class FilterReader
       extends Reader
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.FilterReader class −

    • protected Reader in − This is the character-input stream.
    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1protected FilterReader(Reader in)This creates a new filtered reader.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream and releases any system resources associated with it.
    2void mark(int readAheadLimit)This method marks the present position in the stream.
    3boolean markSupported()This method tells whether this stream supports the mark() operation.
    4int read()This method reads a single character.
    5int read(char[] cbuf, int off, int len)This method reads characters into a portion of an array.
    6boolean ready()This method tells whether this stream is ready to be read.
    7void reset()This method resets the stream.
    8long skip(long n)This method skips characters.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Reader
    • Java.io.Object
  • FilterOutputStream

    Introduction

    The Java.io.FilterOutputStream class is the superclass of all classes that filter output streams. Following are the important points about FilterOutputStream −

    • The class itself simply overrides all methods of OutputStream with versions that pass all requests to the contained output stream.
    • The Subclasses of this class may further override some of these methods and may also provide additional methods and fields.

    Class declaration

    Following is the declaration for Java.io.FilterOutputStream class −

    public class FilterOutputStream
       extends OutputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.FilterOutputStream class −

    • protected OutputStream out − This is the output stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1FilterOutputStream(OutputStream out)This creates an output stream filter built on top of the specified underlying output stream.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this output stream and releases any system resources associated with the stream.
    2void flush()This method flushes this output stream and forces any buffered output bytes to be written out to the stream.
    3void write(byte[] b)This method writes b.length bytes to this output stream.
    4void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this output stream.
    5void write(int b)This method writes the specified byte to this output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • FilterInputStream 

    Introduction

    The Java.io.FilterInputStream class contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality. Following are the important points about FilterInputStream −

    • The class itself simply overrides all methods of InputStream with versions that pass all requests to the contained input stream.
    • The Subclasses of this class may further override some of these methods and may also provide additional methods and fields.

    Class declaration

    Following is the declaration for Java.io.FilterInputStream class −

    public class FilterInputStream
       extends InputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.FilterInputStream class −

    • protected InputStream in − This is the input stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1protected FilterInputStream(InputStream in)This creates a FilterInputStream by assigning the argument in to the field this.in to remember it for later use.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
    2void close()This method closes this input stream and releases any system resources associated with the stream.
    3void mark(int readlimit)This method marks the current position in this input stream.
    4boolean markSupported()This method tests if this input stream supports the mark and reset methods.
    5int read()This method reads the next byte of data from this input stream.
    6int read(byte[] b)This method reads up to byte.length bytes of data from this input stream into an array of bytes.
    7int read(byte[] b, int off, int len)This method reads up to len bytes of data from this input stream into an array of bytes.
    8void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
    9long skip(long n)This method skips over and discards n bytes of data from this input stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • FileWriter 

    Introduction

    The Java FileWriter class is a convenience class for writing character files.Following are the important points about FileWriter −

    • The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.
    • FileWriter is meant for writing streams of characters. For writing streams of raw bytes, use FileOutputStream.

    Class declaration

    Following is the declaration for Java.io.FileWriter class −

    publicclassFileWriterextendsOutputStreamWriter

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.FileWriter class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1FileWriter(File file)This constructor creates a FileWriter object given a File object.
    2FileWriter(File file, boolean append)This constructor creates a FileWriter object given a File object with a boolean indicating whether or not to append the data written.
    3FileWriter(FileDescriptor fd)This constructor creates a FileWriter object associated with the given file descriptor.
    4FileWriter(String fileName)This constructor creates a FileWriter object, given a file name.
    5FileWriter(String fileName, boolean append)This constructor creates a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

    Once you have FileWriter object in hand, then there is a list of helper methods, which can be used to manipulate the files.

    Sr.No.Method & Description
    1public void write(int c) throws IOExceptionWrites a single character.
    2public void write(char [] c, int offset, int len)Writes a portion of an array of characters starting from offset and with a length of len.
    3public void write(String s, int offset, int len)Write a portion of a String starting from offset and with a length of len.

    Example 1

    The following example shows the usage of Java FileWriter class. We’ve created a File reference with name Hello1.txt to be created in current directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.

    Open Compiler

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char[] a =newchar[50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    

    Example 2

    The following example shows the usage of Java FileWriter class. We've created a File reference with name Hello1.txt to be created in a provided directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("F:/Test2/Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    
  • FileReader 

    Introduction

    The Java.io.FileReader class is a convenience class for reading character files.Following are the important points about FileReader −

    • The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.
    • FileReader is meant for reading streams of characters. For reading streams of raw bytes, use FileInputStream.

    Class declaration

    Following is the declaration for Java.io.FileReader class −

    publicclassFileReaderextendsInputStreamReader

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.FileReader class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1FileReader(File file)This constructor creates a new FileReader, given the File to read from.
    2FileReader(FileDescriptor fd)This constructor creates a new FileReader, given the FileDescriptor to read from.
    3FileReader(String fileName)This constructor creates a new FileReader, given the name of the file to read from.

    Once you have FileReader object in hand then there is a list of helper methods which can be used to manipulate the files.

    Sr.No.Method & Description
    1public int read() throws IOExceptionReads a single character. Returns an int, which represents the character read.
    2public int read(char [] c, int offset, int len)Reads characters into an array. Returns the number of characters read.

    Example 1

    The following example shows the usage of Java FileReader class. We’ve created a File reference with name Hello1.txt to be created in current directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.

    Open Compiler

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    

    Example 2

    The following example shows the usage of Java FileReader class. We've created a File reference with name Hello1.txt to be created in a provided directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.

    packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("F:/Test2/Hello1.txt");// creates the file
    
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char&#91;] a =newchar&#91;50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}</code></pre>

    Output

    This
    is
    an
    example
    
  • FilePermission 

    Introduction

    The Java.io.FilePermission class represents access to a file or directory.It consists of a pathname and a set of actions valid for that pathname. Following are the important points about FilePermission −

    • The actions to be granted are passed to the constructor in a string containing a list of one or more comma-separated keywords. The possible keywords are “read”, “write”, “execute”, and “delete”.
    • Code can always read a file from the same directory it’s in (or a subdirectory of that directory); it does not need explicit permission to do so.

    Class declaration

    Following is the declaration for Java.io.FilePermission class −

    public final class FilePermission
       extends Permission
    
      implements Serializable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1FilePermission(String path, String actions)This creates a new FilePermission object with the specified actions.

    Class methods

    Sr.No.Method & Description
    1boolean equals(Object obj)This method checks two FilePermission objects for equality.
    2String getActions()This method returns the “canonical string representation” of the actions.
    3int hashCode()This method returns the hash code value for this object.
    4boolean implies(Permission p)This method checks if this FilePermission object “implies” the specified permission.
    5PermissionCollection newPermissionCollection()This method returns a new PermissionCollection object for storing FilePermission objects.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Permission
    • Java.io.Object
  • FileOutputStream 

    Introduction

    The Java.io.FileOutputStream class is an output stream for writing data to a File or to a FileDescriptor. Following are the important points about FileOutputStream −

    • This class is meant for writing streams of raw bytes such as image data.
    • For writing streams of characters, use FileWriter.

    Class declaration

    Following is the declaration for Java.io.FileOutputStream class −

    public class FileOutputStream
       extends OutputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1FileOutputStream(File file)This creates a file output stream to write to the file represented by the specified File object.
    2FileOutputStream(File file, boolean append)This creates a file output stream to write to the file represented by the specified File object.
    3FileOutputStream(FileDescriptor fdObj)This creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
    4FileOutputStream(String name)This creates an output file stream to write to the file with the specified name.
    5FileOutputStream(String name, boolean append)This creates an output file stream to write to the file with the specified name.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this file output stream and releases any system resources associated with this stream.
    2protected void finalize()This method cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.
    3FileChannel getChannel()This method returns the unique FileChannel object associated with this file output stream.
    4FileDescriptor getFD()This method returns the file descriptor associated with this stream.
    5void write(byte[] b)This method writes b.length bytes from the specified byte array to this file output stream.
    6void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this file output stream.
    7void write(int b)This method writes the specified byte to this file output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.OutputStream
    • Java.io.Object
  • FileInputStream 

    Introduction

    The Java.io.FileInputStream class obtains input bytes from a file in a file system. What files are available depends on the host environment. Following are the important points about FileInputStream −

    • This class is meant for reading streams of raw bytes such as image data.
    • For reading streams of characters, use FileReader.

    Class declaration

    Following is the declaration for Java.io.FileInputStream class −

    public class FileInputStream
       extends InputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1FileInputStream(File file)This creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
    2FileInputStream(FileDescriptor fdObj)This creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
    3FileInputStream(String name)This creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
    2void close()This method closes this file input stream and releases any system resources associated with the stream.
    3protected void finalize()This method ensures that the close method of this file input stream is called when there are no more references to it.
    4FileChannel getChannel()This method returns the unique FileChannel object associated with this file input stream.
    5FileDescriptor getFD()This method returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
    6int read()This method reads a byte of data from this input stream.
    7int read(byte[] b)This method reads up to b.length bytes of data from this input stream into an array of bytes.
    8int read(byte[] b, int off, int len)This method reads up to len bytes of data from this input stream into an array of bytes.
    9long skip(long n)This method skips over and discards n bytes of data from the input stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.InputStream
    • Java.io.Object
  • FileDescriptor 

    Introduction

    The Java.io.FileDescriptor class instances serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. Following are the important points about FileDescriptor −

    • The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.
    • Applications should not create their own file descriptors.

    Class declaration

    Following is the declaration for Java.io.FileDescriptor class −

    public final class FileDescriptor
       extends Object
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.FileDescriptor class −

    • static FileDescriptor err − This is the handle to the standard error stream.
    • static FileDescriptor in − This is the handle to the standard input stream.
    • static FileDescriptor out − This is the handle to the standard output stream.

    Class constructors

    Sr.No.Constructor & Description
    1FileDescriptor()This method constructs an (invalid) FileDescriptor object.

    Class methods

    Sr.No.Method & Description
    1void sync()This method force all system buffers to synchronize with the underlying device.
    2boolean valid()This method tests if this file descriptor object is valid.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • Java – File Class

    Java File Class

    Java File class represents the files and directory pathnames in an abstract manner. This class is used for creation of files and directories, file searching, file deletion, etc.

    The File object represents the actual file/directory on the disk.

    File Class Constructors

    Following is the list of constructors to create a File object.

    Sr.No.Method & Description
    1File(File parent, String child)This constructor creates a new File instance from a parent abstract pathname and a child pathname string.
    2File(String pathname)This constructor creates a new File instance by converting the given pathname string into an abstract pathname.
    3File(String parent, String child)This constructor creates a new File instance from a parent pathname string and a child pathname string.
    4File(URI uri)This constructor creates a new File instance by converting the given file: URI into an abstract pathname.

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    File Class Methods

    Once you have File object in hand, then there is a list of helper methods which can be used to manipulate the files.

    Sr.No.Method & Description
    1public String getName()Returns the name of the file or directory denoted by this abstract pathname.
    2public String getParent()Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
    3public File getParentFile()Returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
    4public String getPath()Converts this abstract pathname into a pathname string.
    5public boolean isAbsolute()Tests whether this abstract pathname is absolute. Returns true if this abstract pathname is absolute, false otherwise.
    6public String getAbsolutePath()Returns the absolute pathname string of this abstract pathname.
    7public boolean canRead()Tests whether the application can read the file denoted by this abstract pathname. Returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.
    8public boolean canWrite()Tests whether the application can modify to the file denoted by this abstract pathname. Returns true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
    9public boolean exists()Tests whether the file or directory denoted by this abstract pathname exists. Returns true if and only if the file or directory denoted by this abstract pathname exists; false otherwise.
    10public boolean isDirectory()Tests whether the file denoted by this abstract pathname is a directory. Returns true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise.
    11public boolean 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. Returns true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise.
    12public long lastModified()Returns the time that the file denoted by this abstract pathname was last modified. Returns a long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs.
    13public long length()Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
    14public boolean createNewFile() throws IOExceptionAtomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. Returns true if the named file does not exist and was successfully created; false if the named file already exists.
    15public boolean delete()Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Returns true if and only if the file or directory is successfully deleted; false otherwise.
    16public void deleteOnExit()Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
    17public String[] list()Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
    18public String[] list(FilenameFilter filter)Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
    20public File[] listFiles()Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
    21public File[] listFiles(FileFilter filter)Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
    22public boolean mkdir()Creates the directory named by this abstract pathname. Returns true if and only if the directory was created; false otherwise.
    23public boolean mkdirs()Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Returns true if and only if the directory was created, along with all necessary parent directories; false otherwise.
    24public boolean renameTo(File dest)Renames the file denoted by this abstract pathname. Returns true if and only if the renaming succeeded; false otherwise.
    25public boolean setLastModified(long time)Sets the last-modified time of the file or directory named by this abstract pathname. Returns true if and only if the operation succeeded; false otherwise.
    26public boolean setReadOnly()Marks the file or directory named by this abstract pathname so that only read operations are allowed. Returns true if and only if the operation succeeded; false otherwise.
    27public static File createTempFile(String prefix, String suffix, File directory) throws IOExceptionCreates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. Returns an abstract pathname denoting a newly-created empty file.
    28public static File createTempFile(String prefix, String suffix) throws IOExceptionCreates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null). Returns abstract pathname denoting a newly-created empty file.
    29public int compareTo(File pathname)Compares two abstract pathnames lexicographically. Returns zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argument.
    30public int compareTo(Object o)Compares this abstract pathname to another object. Returns zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argument.
    31public boolean equals(Object obj)Tests this abstract pathname for equality with the given object. Returns true if and only if the argument is not null and is an abstract pathname that denotes the same file or directory as this abstract pathname.
    32public String toString()Returns the pathname string of this abstract pathname. This is just the string returned by the getPath() method.

    File Class Example in Java

    Following is an example to demonstrate File object −

    Open Compiler

    packagecom.tutorialspoint;importjava.io.File;publicclassFileDemo{publicstaticvoidmain(String[] args){File f =null;String[] strs ={"test1.txt","test2.txt"};try{// for each string in string array for(String s:strs ){// create new file
    
            f =newFile(s);// true if the file is executableboolean bool = f.canExecute();// find the absolute pathString a = f.getAbsolutePath();// prints absolute pathSystem.out.print(a);// printsSystem.out.println(" is executable: "+ bool);}}catch(Exception e){// if any I/O error occurs
         e.printStackTrace();}}}</code></pre>

    Consider there is an executable file test1.txt and another file test2.txt is non executable in the current directory. Let us compile and run the above program, This will produce the following result −

    Output

    /home/cg/root/2880380/test1.txt is executable: false
    /home/cg/root/2880380/test2.txt is executable: false