Java Program to Delete File in Java

Java Program to Delete File in Java

What are File System?

In Java, a file system is a set of classes and interfaces that provide a standard way for reading and writing files and directories. The most commonly used file system classes are those in the java.io package, such as File, FileInputStream, and FileOutputStream.

In this Article, we will write a program to Count number of lines present in the file.

File System in Java :

In Java, the file system can be accessed and manipulated through the java.io and java.io packages.

The java.io package provides classes such as File, FileInputStream, and FileOutputStream for working with files and directories. The File class can be used to create, delete, and manipulate files and directories. The FileInputStream and FileOutputStream classes can be used to read and write data to files, respectively.
The java.io package, which was introduced in Java 1.4, provides advanced file system functionality such as file channel I/O and memory-mapped file I/O. The java.nio package includes classes such as Path, FileChannel, and MappedByteBuffer that can be used to perform low-level file system operations such as reading, writing, and manipulating file metadata.

Additionally, the java.io.file package, which was introduced in Java 7, provides a more modern and versatile file system API. The java.nio.file package includes classes such as the Path, Files, and FileSystem classes, which can be used to perform various file system operations, including reading, writing, and manipulating file metadata.

Program to Delete File :

Run
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        // File path
        String filePath = "path/to/file.txt";

        // Variable to store the number of lines
        int lineCount = 0;

        try {
            // Read all lines from the file
            lineCount = (int) Files.lines(Paths.get(filePath)).count();
            // Print the number of lines
            System.out.println("Number of lines in the file: " + lineCount);
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        }
    }
}

Explanation:

The above program uses the Files.lines() method from java.nio.file package which returns a stream of all lines from the file, and the count() method is used to count the number of lines. The method returns a null when the end of the file is reached. Finally, the number of lines is printed to the console.

Note: In this sample code above, you need to replace the path/to/file.txt with the actual path of the file you want to count the lines from.

Program to Delete File :

Run
import java.io.*;

public class Main {
    public static void main(String[] args) {
        // File path
        String filePath = "path/to/file.txt";

        // Variable to store the number of lines
        int lineCount = 0;

        try {
            // FileReader to read the file
            FileReader fr = new FileReader(filePath);

            // BufferedReader to read the file line by line
            BufferedReader br = new BufferedReader(fr);

            // Read the file line by line
            while (br.readLine() != null) {
                lineCount++;
            }

            // Close the BufferedReader
            br.close();

            // Print the number of lines
            System.out.println("Number of lines in the file: " + lineCount);
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        }
    }
}

Explanation:

This program uses the FileReader and BufferedReader classes to read the file line by line. The readLine() method of the BufferedReader class is used to read the file line by line. Each time a line is read, the lineCount variable is incremented by 1. The program also includes exception handling for the case when the file is not found or can’t be read. The FileNotFoundException is thrown when the file is not found, and the IOException is thrown when there is an error reading the file.

Note: In this sample code above, you need to replace the path/to/file.txt with the actual path of the file you want to count the lines from.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription