Java file object


 

Java file object

This section demonstrates you the concept of File object.

This section demonstrates you the concept of File object.

Java file object

This section demonstrates you the concept of File object.

Java.io.* package has provide a number of useful  tools which has made the programming much easier. Here we will discus the operations performed by File object.

Using the File object, you can perform several operations. It parses the file to be used for reading or writing purposes. There are various built in functions which shows their utility by just calling them through the object, like creating a file, deleting a file, getting last modification date, renaming a file, getting file name, getting file path, creating a temporary file, creating a directory etc. We have mentioned few of them in the given example.

Here is the code:

import java.io.*;

class FileObject {
	public static void main(String[] args) throws Exception {
		File f = new File("c:/newFile.txt");
		System.out.println("Create file: " + f.createNewFile());
		System.out.println("Filename: " + f.getName());
		System.out.println("Length: " + (f.length()) + "bytes");
		System.out.println("Last modified timesptamp: " + f.lastModified());
		System.out.println("File is renamed to hello.txt: "
				+ f.renameTo(new File("C:/hello.txt")));
	}
}

Ads