Write a java program to Get file size and file path


This Java code defines a class File_SizePath with a main method that retrieves the size and path of a file named "file.txt" in the current working directory.

The try block creates a new File object using the fileName string, which is then used to retrieve the absolute path of the file using the getAbsolutePath method. The size of the file in bytes is also retrieved using the length method of the File class.

If an exception occurs, the catch block will print out the message "Exception" along with the error message provided by the exception object.

Note that this code assumes that the file "file.txt" exists in the current working directory, and will throw a FileNotFoundException if the file cannot be found.

Source Code

import java.io.File;
public class File_SizePath
{
	public static void main(String args[])
	{
		final String fileName = "file.txt";
		try
		{
			File objFile = new File(fileName);
			System.out.println("File Path : " + objFile.getAbsolutePath());
			System.out.println("File Size : " + objFile.length() + " bytes");
		}
		catch (Exception e)
		{
			System.out.println("Exception : " + e.toString());
		}
	}
}

Output

File Path : C:\Users\tutor\Desktop\JAVA\FILEHANDLING\file.txt
File Size : 16 bytes