# Display the length of current file in Java

Java 8Object Oriented ProgrammingProgramming

The length of the current file specified by the abstract path name can be displayed using the method java.io.File.length(). The method returns the file length in bytes and does not require any parameters.

A program that demonstrates this is given as follows −

## Example

Live Demo

import java.io.File;
public class Demo {
public static void main(String[] args) {
try {
File file = new File("demo1.txt");
file.createNewFile();
System.out.println("Length of file: " + file.length());
} catch(Exception e) {
e.printStackTrace();
}
}
}

The output of the above program is as follows −

## Output

Length of file: 0

Now let us understand the above program.

The method java.io.File.length() is used to find the length of the current file specified by the abstract path name and display it. A code snippet that demonstrates this is given as follows −

try {
File file = new File("demo1.txt");
file.createNewFile();
System.out.println("Length of file: " + file.length());
} catch(Exception e) {
e.printStackTrace();
}
Published on 25-Feb-2019 07:41:51