×

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations on files and directories. It is a representation of file and directory pathnames that is abstract. Instances of the File class are immutable, which means that once generated, a File object's abstract pathname will never change. This class comes under the java.io package, and it implements the following interfaces.

  • Serializable
  • Comparable<File>

File and Directory

A file is a name for a location where relevant data can be saved. And a directory is a part or place in your computer’s memory where you can store files. It is implemented to perform two functions: to store other directories ( known as subdirectories) and helps keep the computer organized.

File Class Constructors

File class provides the following constructors, such as:

1

File(Filepnt, String cld)

This constructor is implemented when there is a need to create a new file instance that must include the following parameters: parent abstract pathname and a child pathname string.

2

File(Stringpathn)

When a new File instance must be created, this constructor is implemented to turn the provided pathname string into an abstract pathname.

3

File(String pnt, String cld)

This constructor is implemented when there is a need to create a new file instance must be created by using three parameters: string, parent pathname, and child pathname.

4

File(URI u)

This constructor is used when a new file instance has to be generated by transforming the following parameters: URI into an abstract pathname.

File Class Methods

File class provides the following methods.

  1. canExecute()

This function is implemented when it is necessary to check whether the application can carry out its function to execute the file represented by this abstract pathname. This method has a boolean return type.

  1. canRead()

This function is implemented when it is necessary to check whether the application can carry out its function to read the file represented by this abstract pathname. This method has a boolean return type.

  1. canWrite()

This function is implemented when it is necessary to check whether the application can carry out its function to update the file represented by this abstract pathname. This method has a boolean return type.

  1. compareTo(Filepname)

This method is implemented when there is a need to differentiate two abstract pathnames lexicographically. This method has an int return type.

  1. createNewFile()

This method is implemented when there is a need to generate a new empty file, represented by this abstract pathname. This method has a boolean return type.

  1. createTempFile(String pf, String sf )

This method is implemented when there is a need to generate a new file that is empty and has a default temporary-file directory. This method has a file return type.

  1. delete()

This method is implemented when there is a need to delete the file or directory represented by this abstract pathname. This method has a boolean return type.

  1. equals(Object o)

This method is implemented when there is a need to examine the abstract pathname for equality with the specified object. This method has a boolean return type.

  1. exists()

This method is implemented when there is a need to examine whether the file or directory denoted by this abstract pathname exists. This method has a boolean return type.

  1. getAbsolutePath()

This function is implemented when the absolute pathname string of this abstract pathname must be returned. This method has a String return type.

  1. list()

As its name implies, this method produces an array of strings naming the files and folders in the directory. This method has a String[ ] return type.

  1. getFreeSpace()

This function is used when the number of unallocated bytes in the partition must be returned. This method has a long return type.

  1. getName()

This method is implemented when there is a need to return the file or directory name denoted by this abstract pathname. This method has a String return type.

  1. getParent()

This method is implemented when there is a need to return the pathname string of this abstract pathname’s parent. This method has a String return type.

  1. getParentFile()

This method is implemented when there is a need to return the abstract pathname of this abstract pathname’s parent. This method has a String return type.

  1. getPath()

This method is implemented when there is a need to convert this abstract pathname into a pathname string. This method has a String return type.

  1. setReadOnly()

This method is implemented when there is a need to mark the file or directory name, ensuring that only read operations can be implemented. This method has a boolean return type.

  1. isDirectory()

This method is implemented when there is a need to examine the file denoted by this pathname as a directory. This method has a String return type.

  1. isFile()

This method is implemented when there is a need to examine the file denoted by this abstract pathname as a normal file. This method has a String return type.

  1. isHidden()

This method is implemented when there is a need to examine whether the file represented by this abstract pathname is hidden. This method has a String return type.

  1. length( )

As the function's name implies, this function returns the length. The length belongs to the file denoted by this abstract pathname. This method has a long return type.

  1. listFiles( )

This function returns an array that belongs to the abstract pathnames, which indicate the particular files in the directory. This method has a file[ ] return type.

  1. mkdir()

This function generates a  directory named by this abstract pathname. This method has a String return type.

  1. renameTo(File dt )

As the name implies, this function renames the file represented by this abstract pathname. This method has a boolean return type.

  1. setExecutable(boolean executable)

This function is a  quick way to change the owner's execute permission. This method has a String return type.

  1. setReadable(boolean readable)

This function is a  quick way to change the owner's read permission. This method has a String return type.

  1. setReadable(boolean rd, boolean only)

This function is a  quick way to change the owner's, or everybody’s read permission. This method has a boolean return type.

  1. setWritable(boolean w)

This function is a  quick way to change the owner's write permissions. This method has a boolean return type.

  1. toString()

This method is implemented when there is a need to return the pathname, which is a string type belonging to this abstract pathname. This method has a String return type.

  1. toURI()

This method is implemented when it's necessary to produce a file URI that indicates this abstract pathname. This method has a URI return type.


Related Topics

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Array Slicing in Java

Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] =...

3 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.