×

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

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.