How CRUD operations are used for file handling

Java is a high level language. Java programs are usually executed on a terminal that is not stored; Some programs require a source to store the information received from the program in the form of a file. CRUD stands for Operations to create, Reading, Updates And delete,

As said, some Java programs require certain files to store the information received from the program. A file stores different types of information including text, images, videos, etc.

We can perform several operations on the file:

  • Nature
  • Initial
  • Reading
  • Writing
  • moving
  • ending

Let us discuss CRUD operations in a better way using example code.

CRUD operations

What does CRUD mean? to create, read/retrieve, Updates And delete, These are used in file handling in Java. The creation of a file is done using POST, the reading of a file is done using GET, the file is updated using PUT, and to delete a file, we use DELETE.

Let us now discuss each operation with an example using Java.

First, let’s discuss Nature of file:

  • To create a file in Java, we use the createNewFile() method.
  • If the file is created successfully it returns a boolean value which is TRUE; Otherwise returns FALSE.

Code:

import java.io.File;  

import java.io.IOException;   

 class cfile {  

               public static void main(String args[]) {  

               try {  

                       File file = new File("C:JavaPrograms/sample.txt");   

                       if (file.createNewFile()) {  

                                  System.out.println("File " + file.getName() + " is created successfully.");  

                       } else {  

                                  System.out.println("File is already exists");  

                       }  

                     } catch (IOException exception) {  

                              System.out.println("Exception Occurred");  

                  }   

        }  

} 

Output:

The file sample.txt is created successfully.

let us to create One more file for better understanding.

import java.io.File;  

import java.io.IOException;   

 class cfile1 {  

               public static void main(String args[]) {  

               try {  

                       File file = new File("C:JavaPrograms/sample1.txt");   

                       if (file.createNewFile()) {  

                                  System.out.println("File " + file.getName() + " is created successfully.");  

                       } else {  

                                  System.out.println("File is already exists");  

                       }  

                     } catch (IOException exception) {  

                              System.out.println("Exception Occurred");  

                  }   

        }  

} 

Output:

The file sample1.txt is created successfully.

Now let’s discuss Reading either Retrieving file.

To read or retrieve a file in Java, we use the Scanner class and that helps to read the contents of the file.

Code:

import java.io.File;   

import java.io.FileNotFoundException;   

import java.util.Scanner;   

class rfile {  

    public static void main(String[] args) {  

        try {  

            File file = new File("C:JavaPrograms/sample.txt");    

            Scanner dataReader = new Scanner(file);  

            while (dataReader.hasNextLine()) {  

                String fileData = dataReader.nextLine();  

                System.out.println(fileData);  

            }  

            dataReader.close();  

        } catch (FileNotFoundException exception) {  

            System.out.println("Exception Occurred");    

        }  

    }  

}  

Output:

# Reads the file from the mentioned location.

This is a java file used as a sample!

another example for Reading Sample 1. Text file.

Code:

import java.io.File;   

import java.io.FileNotFoundException;   

import java.util.Scanner;   

class rfile1 {  

    public static void main(String[] args) {  

        try {  

            File file = new File("C:JavaPrograms/sample1.txt");    

            Scanner dataReader = new Scanner(file);  

            while (dataReader.hasNextLine()) {  

                String fileData = dataReader.nextLine();  

                System.out.println(fileData);  

            }  

            dataReader.close();  

        } catch (FileNotFoundException exception) {  

            System.out.println("Exception Occurred");    

        }  

    }  

}  

Output:

# Reads the file from the mentioned location.

This is a java file, used as sample 1 for better understanding!

Now let’s discuss Writing either to update file,

To write or update a file in Java, we use FileWriter class with Write() method and use it to write or update some content in the file.

Code:

import java.io.FileWriter;   

import java.io.IOException;   

class ufile {  

    public static void main(String[] args) {  

    try {  

        FileWriter fwrite = new FileWriter("C:JavaPrograms/sample.txt");  

        fwrite.write("I’m updating the file with this statement!");   

        fwrite.close();   

        System.out.println("Content is successfully wrote to the file.");  

    } catch (IOException e) {  

            System.out.println("Exception Occurred");     

        }  

    }  

}  

Output:

# Update the file from the mentioned location.

I am updating the file with this statement!

now to update sample1.txt file,

import java.io.FileWriter;   

import java.io.IOException;   

class ufile {  

    public static void main(String[] args) {  

    try {  

        FileWriter fwrite = new FileWriter("C:JavaPrograms/sample.txt");  

        fwrite.write("I’m updating the file with this statement!");   

        fwrite.close();   

        System.out.println("Content is successfully wrote to the file.");  

    } catch (IOException e) {  

            System.out.println("Exception Occurred");     

        }  

    }  

}  

Output:

# Update the file from the mentioned location.

I am updating sample 1 file with this statement!

Now let’s discuss remove file.

To delete a file in Java, we use the delete() method, which is used to delete files.

Code:

import java.io.File;   

class DeleteFile {  

  public static void main(String[] args) {   

    File file= new File("C:JavaPrograms/sample.txt");   

    if (file.delete()) {   

      System.out.println(file.getName()+ " file is deleted successfully.");  

    } else {  

            System.out.println("Error Occurred");    

 

    }   

  }   

}  

Output:

# Deletes the file from the specified location.

The sample.txt file has been deleted successfully.

now we delete The sample1.txt file from the JavaPrograms folder in C drive.

Code:

import java.io.File;   

class DeleteFile {  

  public static void main(String[] args) {   

    File file= new File("C:JavaPrograms/sample1.txt");   

    if (file.delete()) {   

      System.out.println(file.getName()+ " file is deleted successfully.");  

    } else {  

            System.out.println("Error Occurred");    

 

    }   

  }   

}  

Output:

# Deletes the file from the specified location.

sample1 The .txt file has been deleted successfully.

file handling in java

We have some streams in Java which are used to perform input-output operations on files. Stream is nothing but sequence of data.

Here we have two types of streams in Java:

  1. input stream
  2. output stream

In Java, we also have some File class methods like createNewFile(), canRead(), canWrite(), delete(), and more.

conclusion

  1. We discussed how CRUD operations are used in Java.
  2. Also discussed why CRUD operations are used in Java.
  3. CRUD operations are CREATE, READ, UPDATE and DELETE.
  4. Basically, CRUD operations are used to handle files in Java.
  5. To create the file C is used to create the file using Java.
  6. R to read a file or retrieve a file using java.
  7. U to update the file using java.
  8. D to delete the file using java.
  9. We can perform many other operations on files using Java.
  10. We have discussed various examples using Java Programs for better understanding of the concepts.

I hope this blog was informative.

Leave a Comment