Advertisement
File(String path) |
---|
This example creates a File object, which is referencing a file TextBook.txt in D: drive, by a String path -"D://TextBook.txt". |
File(File parent, String child) |
---|
This example creates a new file TextBook.txt at a path assigned to parent File object, dir - "D://".
Hence, file TextBook.txt will be created in D: Drive. |
Methods | Description |
---|---|
boolean createNewFile() | This method creates a file on the disk. It returns true if file is successfully created and false if it is not. |
boolean exists() | This method returns true if a file/directory exists and it returns false, if it does not exist. |
String getName() | This method returns the name of the file or a directory. |
String getParent() | This method returns the name of the Parent directory in which a file/directory is located, if there is no parent directory then null is returned. |
boolean isDirectory() | This method returns true if a File object points to a directory otherwise it returns false. |
boolean isFile() | This method returns true if a File object points to a file on disk otherwise it returns false. |
boolean isHidden() | This method returns true if a File object points to a file which is hidden. |
boolean canRead() | This method returns true if a File object points to a file which can be read and has no read lock on it. |
boolean canWrite() | This method returns true if a File object points to a file which can be write to and has no write lock on it. |
long lastModified() | This method returns the time in milliseconds when a file was modified. |
long length() | This method returns the length of a file(in terms of bytes) referenced by File object. |
Advertisement
import java.io.*;
class A
{
public static void main(String... ar)
{
File file= new File("D:\\TextBook1.txt");
try
{ //calling createNewFile() method creates a new File
System.out.println("Is the file created ? "+ file.createNewFile());
}
catch(IOException e)
{
System.out.println(e);
}
System.out.println("Does this file exists? "+file.exists());
System.out.println("The name of file is "+file.getName());
System.out.println("Parent Directory of this file "+file.getParent());
System.out.println("Path to this file "+file.getPath());
System.out.println("The full path to this file "+file.getAbsolutePath());
System.out.println("Is this a Directory? "+file.isDirectory());
System.out.println("Is this a File? "+file.isFile());
System.out.println("Is this a hidden file "+file.isHidden());
System.out.println("Is this File readable ? "+file.canRead());
System.out.println("Is this File writable ? "+file.canWrite());
} //main method ends
} //class definition ends
Does this file exists? true
The name of file is TextBook1.txt
Parent Directory of this file D:\
Path to this file D:\TextBook1.txt
Is this a Directory? false
Is this a File? true
Is this a hidden file false
Is this File readable ? true
Is this File writable ? true
File size ? 0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement