Ad Code

File Handling in Java | Learning Java | Basic Java.

 File Handling in Java 

Hy, In this tutorial, we will learn what is file handling and how to create a file, write to file, and read a file or existing file. And We use the Exception Handling concept.


What is a file?

A file is a collection of similar types of records and is a container in a computer system for storing information.

What is File Handling in Java?

File handling in java enables us to create, update, read, and delete the files stored on the local file system through our java program.

File handling is an important concept of programming languages.

In Java language, the   File  class is imported from the   java.io  package.


Operations can be performed on a file.

  • Creating new file
  • Read file
  • Write into a file
  • Delete a file
Useful Methods:

  • createNewFile(): Create an empty file.
  • delete(): Delete a file.
  • write(): Write to a file.
  • getName(): Return name of the file.

Creating New File:

To create a new file with the help of  createNewFile()  function.
 import java.io.*;
 import java.util.Scanner;
 public class file {
    public static void main(String[] args) {
        // Create a new file
        File myfile=new File("Myfile1.txt");
        try {
            myfile.createNewFile();
            System.out.println("New file Created");

        } catch (IOException e) {
            System.out.println("Finished");
            e.printStackTrace();
        }
    }
 }

Output:

New file Created

Write Into a File:

To Write into a file with the help of  write()  function. We will use  FileWriter  class from java.io package.
 import java.io.*;
 import java.util.Scanner;
 public class file {
    public static void main(String[] args) {
        //Write into a file
        try {
            FileWriter fileWriter =new FileWriter("Myfile.txt");
            fileWriter.write("Hey, this is my new file in created using java\nHue Hue Hue.....");
            fileWriter.close();
            System.out.println("Wrote");
        } 
           catch (IOException e) {
            e.printStackTrace();
        }
    }
 }

Output:

Wrote

Read a File:

To read a file which is already created.

 import java.io.*;
 import java.util.Scanner;
 public class file {
    public static void main(String[] args) {
                //read from the file
        File myfile=new File("quicklab.txt");
        try {
            Scanner sc=new Scanner(myfile);
            while(sc.hasNextLine()){
                String line = sc.nextLine();
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
 }


Output:

I am reading a file.

Delete a File:

To delete a file with the  delete()  function.
 import java.io.*;
 import java.util.Scanner;
 public class file {
    public static void main(String[] args) {
               // delete a file
              File myfile=new File("quicklab.txt");
        if(myfile.delete()){
            System.out.println("File deleted "+myfile.getName());
         }
    }
 }



Output:

File Deleted quicklab.txt

Post a Comment

0 Comments

Ad Code