File length() in Java

length(): This method is available in the java.io.File class of Java.

Syntax:

long java.io.File.length()

This method returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

Parameters: NA

Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.

Throws:

SecurityException - If a security manager exists and its java.lang.SecurityManager.checkRead(java.lang.String) method denies read access to the file

Approach

Java

import java.io.File;

public class Filelength {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.length());
    }
}

Output:

0


No comments:

Post a Comment