File length() method in Java with Examples Last Updated : 09 Oct, 2022 Comments Improve Suggest changes Like Article Like Report The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length.The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs. Function signature: public long length() Syntax: long var = file.length(); Parameters: This method does not accept any parameter. Return Type The function returns long data type that represents the length of the file in bits. Exception: This method throws Security Exception if the write access to the file is denied Below programs illustrate the use of the length() function: Example 1: The file "F:\\program.txt" is an existing file in F: Directory. Java // Java program to demonstrate // length() method of File Class import java.io.*; public class solution { public static void main(String args[]) { // Get the file File f = new File("F:\\program.txt"); // Get the length of the file System.out.println("length: " + f.length()); } } Output: length: 100000 Example 2: The file "F:\\program1.txt" is empty Java // Java program to demonstrate // length() method of File Class import java.io.*; public class solution { public static void main(String args[]) { // Get the file File f = new File("F:\\program.txt"); // Get the length of the file System.out.println("length: " + f.length()); } } Output: length: 0 Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file. Comment More infoAdvertise with us Next Article File length() method in Java with Examples A andrew1234 Follow Improve Article Tags : Java Java-Functions java-file-handling Java-IO package Java-File Class +1 More Practice Tags : Java Similar Reads List get() method in Java with Examples The get() method of List interface in Java is used to get the element present in this list at a given specific index. Example:Java// Java Program to demonstrate List // get() Method import java.util.*; class Main { public static void main (String[] args) { // Create a List List<Integer> a=new 2 min read Field get() method in Java with Examples The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an 3 min read Files size() method in Java with Examples size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. Th 2 min read Files copy() Method in Java with Examples The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files cla 5 min read File lastModified() method in Java with Examples The lastModified() function is a part of File class in Java . This function returns the time denoted by the this abstract pathname was last modified.The function returns long value measured in milliseconds, representing the time the file was last modified else returns 0L if the file does not exists 2 min read Like