The program starts by creating a File object with the name "file.txt". Then, the length() method of the File class is used to get the size of the file in bytes. The file size is stored in the variable s.
Next, the program prints the file size in bytes, kilobytes, and megabytes. To convert the file size to kilobytes and megabytes, the size in bytes is divided by 1024 and 1024*1024 respectively, and the result is printed using the System.out.println() method.
import java.io.*; public class FileSize { public static void main(String[] args) { File file = new File("file.txt"); long s = file.length(); //file size // print file size in bytes,kilobytes and megabytes System.out.println("File Size in Bytes : " + s); System.out.println("File Size in Filobytes : " + (double)s/1024); System.out.println("File Size in Megabytes : " + (double)s/(1024*1024)); } }
File Size in Bytes : 21 File Size in Filobytes : 0.0205078125 File Size in Megabytes : 2.002716064453125E-5
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions