Program
import java.io.File; import java.io.IOException; public class Lengthoffile { public static void main(String[] args) { try { File f = new File("D:\\doc.txt"); if(f.createNewFile()) { System.out.println("file created" + f.getName()); } else { System.out.println("file already exist"); long value=f.length(); System.out.println("length:" + value); } } catch(IOException e) { System.out.println("error occured"); e.printStackTrace(); } } }
Output
file already exist length:0
Description
public long length()
Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
Where it is required to distinguish an I/O exception from the case that 0L is returned, or where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used.
Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.
Throws:
SecurityException – If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file