Java file handling introduction

A File is a collection of related information. In java, there is a class File From package java.io. File handling in Java implies reading and writing data to a file. The File class from the java.io package, allows us to work with different formats of files. In order to use the File class, you need to create an object of the class and specify the filename or directory name.

For example:

import  java.io.File
File f1 = new File(“filename.txt”);
 

Operations On Files:

1) Creating a File

2) Writing a File

3) Reading a File

4) Deleting a File

To Perform above Operations, the file class has some methods:

List of methods of File class:

1.NewFile() : It returns Boolean value True or False. If File is created it returns True value. If File is not created  due to some reasons it returns False value.

For Example:

import java.io.*;
public class Main
{	
	public static void main(String[] args) {
		try {
			File f1 = new File("ABC.txt");
			if(f1.createNewFile())
			{
				System.out.println("File is successfully created");
			}
			else
			{
				System.out.println("File not created");
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}
Output:
File is successfully created

2. Write(): It returns Boolean value True or False. If File is written, it returns True value and it tests whether the file is written or not, otherwise it returns false value.

For example:

import java.io.*;
public class Main
{
	public static void main(String[] args) {
		try {
			FileWriter fw = new FileWriter("ABC.txt");
			fw.write("Files in Java");
			fw.write("features of java are:");
			fw.close();
			System.out.println("Successfully wrote to the file");
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}
Output:
Successfully wrote to the file.

3. Read(): It returns Boolean value True or False. If File is readable, it returns True value otherwise it returns false value.

For example:

import java.io.*;
import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    try {
	        File f1 = new File("ABC.txt");
	        Scanner MyReader = new Scanner(f1);
	        while(MyReader.hasNextLine());
	        {
	            String data = MyReader.nextLine();
	            System.out.println(data);
	        }
	        MyReader.close();
	    }
	    catch(Exception e)
	    {
	        System.out.println(e);
	    }
	}
}
Output:
ABC.txt

4. delete(): It returns Boolean value. It deletes a file.

For Example:

import java.io.*;
public class Main
{
	public static void main(String[] args) {
	    File f1 = new File("ABC.txt");
	       if(f1.delete())
	       {
	           System.out.println("Deleted the file:"+f1.getName());
	       }
	       else{
	           System.out.println("Failed to delete the file:");
	       }
	}
}
Output:
Deleted the File: ABC.txt

Change File Permissions

We have 3 changing file permissions:

1.Executable

2.Readable

3.Writable

Executable: It tests whether the application can execute the file or not. It returns true if and only if the file executed. otherwise it returns false.

Syntax:

public   boolean  canExecute()

Readable: It tests whether the application can read the file or not. It returns true if and only if the file executed. Otherwise it returns false.

Syntax:

Public boolean canRead()

Writable: It  tests whether the application can modify the file or not. It returns true if and only if the file executed. Otherwise it returns false.

Syntax:

Public boolean canWrite()

An example program to check the java file permissions:


import java.io.*;
public class Main
{
	public static void main(String[] args) {
		File file = new File("ABC.txt"); 
		boolean exists = file.exists(); 
		if(exists == true) 
		{
			System.out.println("Executable: " + file.canExecute()); 
			System.out.println("Readable: " + file.canRead()); 
			System.out.println("Writable: "+ file.canWrite()); 
		} 
		else
		{ 
			System.out.println("File not found."); 
		} 
	} 
}
Output:
Executable:false.
Readable:True.
Writable:True