File Operations in Java

In this article, we will teach you how to create, read, update, and delete a file using java also look at some file-related operations.

To do CURD operation in java we need to import the java.io package through this package we manipulate java files and directories

Create a file object:-

To create a text file we need to import the java.io.File package into the program after this we create an object of the File.

Syntax:

File object_name=new File(path_name);

here this object_name is used to work with files and directories

Now create a text file:-

To create a text file we used the createNewFile() method and it returns true if the file is successfully created else it returns false. Note code enclosed in a try...catch block.

import java.io.*;
class CURD{
    public static void main(String[] args) {
        try{
            File fileObject=new File("CreateFile.txt");
            if(fileObject.createNewFile()) System.out.println("Sucessefully created file "+ fileObject.getName());
            else System.out.print("Already a file occured");
        }catch(IOException exception){
            System.out.println("Error occured !!! ");
            exception.printStackTrace();
        }
    }
}
Output:-
Sucessefully created file CreateFile.txt


Now write in a text file:-

We need to import the java.io.FileWriter package into the program after this we create an object of the FileWriter.

Syntax:

FileWriter object_name=new FileWrite();

To write in a text file we used the write() method. Note code enclosed in a try...catch block.

import java.io.*;
class CURD{
    public static void main(String[] args) {
        try{
            FileWriter writeFile=new FileWriter("CreateFile.txt");
            writeFile.write("This is the method to write in a file. ");
            writeFile.close();
            System.out.print("Successfully done ");
        }catch(IOException exception){
            exception.printStackTrace();
        }
    }
}

Output:

Successfully done.


Now read a text file:-

We need to import the java.io.File package into the program after this we create an object of the File.

Syntax:

File object_name=new File();

To read a text file we used the Scanner class. Note code enclosed in a try...catch block.

import java.io.*;
import java.util.*;
class CURD{
    public static void main(String[] args) {
        try{
            File readFile=new File("CreateFile.txt");
            Scanner sc=new Scanner(readFile);
            while(sc.hasNextLine()){
                String data=sc.nextLine();
                System.out.println("\n"+data);
            }
        }catch(FileNotFoundException exception){
            exception.printStackTrace();
        }
    }
}

Output:

This is the method to write in a file. 


Now delete a text file:-

To delete a text file we used the delete() method.

import java.io.*;
class CURD{
    public static void main(String[] args) {
        File deleteFile=new File("CreateFile.txt");
            if(deleteFile.delete()){
                System.out.println(deleteFile.getName()+" this file deleted");
            }
            else{
                System.out.print("Failed to delete!!! ");
            }
    }
}

Output:

CreateFile.txt this file deleted


//------------------------------Bonus for You------------------------------//

How to become a successful YouTuber?

If you want to become a successful youtube in your life so you have to know everything about youtube like how SEO works, how to rank your video on youtube, how to get more traffic, and so on. But for beginners, it is complicated to rank your video on youtube, to get traffic on your youtube channel, and more. So don't worry about it. if you want to become a YouTuber we can help you. Click here and buy the youtube manifest program at an affordable price. This program helps you to become a successful YouTuber and the result of this program was very good.


Post a Comment

0 Comments