New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

File Handling


File Class

In Java, File class is used for the representation of files or directory pathname. Because of the names of files and directory have different formats on a different platform. The path of a file can be absolute or relative. There are many methods of file classes which can be used for reading a file, deleting a file or renaming a file etc.

Following are the fields of File Class.

S.no Field Description
1 pathSeparator It is represented as a string and it is used to separate the characters of the path.
2 pathSeparatorChar It is used as a path separator character.
3 separator It is represented as a string and it is used to separate the characters of the path. It is a default name separator.
4 separatorChar It is represented as a string and it is used to separate the characters of the path.It is a default name separator.

Following are the Constructors of File Class.

1. File(File parent, String child)

2. File(String pathname)

3. File(String parent, String child)

4. File(URI uri)

Following are the methods of File Class.


S.no. Method Description
1 createTempFile(String prefix, String suffix) It is used to create an empty file.
2 createNewFile() It is used for creating a new file, which is empty and has an abstract pathname.
3 canWrite() It is used to check whether the application can modify a file which has an abstract path.
4 canExecute() It is used to check whether the application can execute a file which has an abstract path.
5 canRead() It is used to check whether the application can read a file which has an abstract path
6 isAbsolute() It is used to check whether the abstract pathname is absolute or not.
7 isDirectory() It is used to check whether the file with abstract pathname is a directory.
8 isFile() It is used to check whether the file with abstract pathname is a file.
9 getName() It is used to get the name of the file.
10 getParent() It is used to get the name of the Parent file
11 toPath() It is used to get objects of java.nio.file.Path.
12 toURI() It is used to create a URL of a file with abstract pathname.
13 listFiles() It is used for getting an array of the abstract pathname.
14 getFressSpace() It is used for getting the number of unallocated bytes.
15 list(FilenameFilter filter) It is used for getting an array of string with the name of the file which have abstract pathname.
16 mkdir() It is used to create directory name.

Creating a File

Example:

	
import java.io.File; 
import java.io.IOException;  

public class FileCreateDemo1
{
	public static void main(String[] args) 
	{
	try 
	{
	File Obj = new File("FileDemo.txt");
	if (Obj.createNewFile()) 
		{
	System.out.println("******File created******");
		System.out.println("Name of the file = " + Obj.getName());
		} 
		else 
		{
	System.out.println("File already exists.");
		}
	} 
	catch (IOException e) 
	{
	e.printStackTrace();
	}
	}
}
	

create-file


Writing in a file

Example:

	
import java.io.FileWriter;   
import java.io.IOException;  

public class FileWriteDemo1 
{
  public static void main(String[] args) 
{
    try 
{
FileWriterobj = new FileWriter("FileDemo.txt");
obj.write("Welcome to studytonight.com.");
obj.close();
System.out.println("File is Updated.");
	}
      catch (IOException e) 
	{
e.printStackTrace();
	}
  }
}
	

write-file
write-file


Reading a file

Example:

	
import java.io.File;  
import java.io.FileNotFoundException;  
import java.util.Scanner; 

public class FileReadDemo1
{
  public static void main(String[] args) 
	{
	try 
	{
		File Obj = new File("FileDemo.txt");
		Scanner obj1 = new Scanner(Obj);
		while (obj1.hasNextLine()) 
		{
	String obj2 = obj1.nextLine();
	System.out.println(obj2);
		}
		obj1.close();
	} 
	catch (FileNotFoundException e) 
	{
		e.printStackTrace();
	}
	}
}
	

reading-file


Copying a file

Example:

	
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyDemo1
{
    public static void main(String[] args)
    {	
	FileInputStream a = null;
	FileOutputStream b = null;
	try
	{
	    File obj_in =new File("FileDemo.txt");
	    File obj_out =new File("FileDemo1.txt");

	    a = new FileInputStream(obj_in);
	    b = new FileOutputStream(obj_out);

	byte[] buffer = new byte[1024];

	int length;
	    while ((length = a.read(buffer)) > 0)
		{
		b.write(buffer, 0, length);
		}
	a.close();
	b.close();
	System.out.println("File copied successfully!!");
	}
	catch(IOException e)
	{
		e.printStackTrace();
	 }
    }
}
	

copy-file
copy-file


File permissions

Example:

	
import java.io.*; 

public class FilePermissionDemo1
{ 
    public static void main(String[] args) 
    { 
        File a = new File("FileDemo1.txt"); 

boolean b = a.exists(); 
if(b == true) 
        { 
System.out.println("Executable: " + a.canExecute()); 
System.out.println("Readable: " + a.canRead()); 
System.out.println("Writable: "+ a.canWrite()); 
        } 
        else
        { 
System.out.println("File not found."); 
        } 
    } 
}
	

file-permission


Retrieving file information

Example:

	
import java.io.File;  
public class FileInfoDemo1
{ 
  public static void main(String[] args) 
	{
		File Obj = new File("FileDemo1.txt");
		if (Obj.exists()) 
		{
		System.out.println("File name= " + Obj.getName());
		System.out.println("***********************************");
		System.out.println("Absolute path= " + Obj.getAbsolutePath());
		System.out.println("***********************************");
		System.out.println("Writeable= " + Obj.canWrite());
		System.out.println("***********************************");
		System.out.println("Readable= " + Obj.canRead());
		System.out.println("***********************************");
		System.out.println("File size in bytes= " + Obj.length());
		} 
		else 
		{
		System.out.println("file does not exist.");
		}
	}
}
	

retrieve-file


Deleting a file

Example:

	
import java.io.File;  
public class FileDeleteDemo1
{
  public static void main(String[] args) 
	{ 
    File Obj = new File("FileDemo.txt"); 
    if (Obj.delete()) 
	{ 
System.out.println(Obj.getName()+ " has been deleted");
	} 
    else 
	{
System.out.println("Failed");
	} 	
  } 
}
	

delete-file