File Handling in Java

File Handling in Java, Java Input and Output operations, handling text files, create a file, read a file, write a file, and compare two files.

File Handling in Java permits us to create, read, update, and delete the files, which are stored on the local file system.

File (Text File) Handling in Java

User Point of view – Drive, Folder, File (text, doc, excel, pdf, etc,), Database
System Point of view – Everything is a file

Java predefined classes to handle text files (Ex: Notepad)

File Class – Selecting a file (any file)
FileReader – Opening a text file in “Read” mode
FileWriter – Opening a text file in “Write” mode
BufferedReader – Read data from the opened file
BufferedWriter – Write data to the opened file

File External operations (Using File Object): Create, Delete, etc,
File Internal operations: Reading, Writing, etc,

Examples:

1. Create a Folder on Desktop

//Create File object
File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March2021”);
fileObj.mkdir();

2. Delete a Folder

File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March2021”);
fileObj.delete();

3. Create a File

File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March.txt”); //Text file
fileObj.createNewFile();

File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March.pdf”); //PDF file
fileObj.createNewFile();

File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March.doc”);// Word document
fileObj.createNewFile();

File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March.xls”); //Excel file
fileObj.createNewFile();

4. Delete a File

File fileObj = new File (“C:\\Users\\Hello\\Desktop\\March.xls”);
fileObj.delete();


Reading Data from Files

Read a Text File

package package1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Sample {

public static void main(String[] args) throws IOException {
String line;

//Open the specified file in “Read” Mode
FileReader file = new FileReader (“C:\\Users\\Hello\\Desktop\\March.txt”);

//Read Data from the opened file in “Read” mode
BufferedReader br = new BufferedReader(file);

while ((line=br.readLine()) !=null) {
System.out.println(line);
}
br.close();
file.close();

}
}

Read a Text File (Range of Records)

package package1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Sample {

public static void main(String[] args) throws IOException {
String line;

//Open the specified file in “Read” Mode
FileReader file = new FileReader (“C:\\Users\\Hello\\Desktop\\March.txt”);

//Read Data from the opened file in “Read” mode
BufferedReader br = new BufferedReader(file);

int lineCount=0;

while ((line=br.readLine()) !=null) {
lineCount= lineCount+1;

if ((lineCount>3) && (lineCount<8)) {
System.out.println(line);
}
}
br.close();
file.close();

}
}


Writing Data to Files

Write Data to a Text File

package package1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Sample {

public static void main(String[] args) throws IOException {
// Open the Specified text file in “Write” Mode
FileWriter file = new FileWriter(“C:\\Users\\Hello\\Desktop\\March.txt”);

//Write data to the opened file in Write mode.
BufferedWriter bw = new BufferedWriter(file);

String data = “Automated Testing using Selenium”;
bw.write(data);
bw.close();

}
}

Read a Text File and write the data to another file

package package1;

import java.io.*;

public class Sample {

public static void main(String[] args) throws IOException {
FileReader file1 = new FileReader(“C:\\Users\\Hello\\Desktop\\abcd.txt”);
FileWriter file2 = new FileWriter (“C:\\Users\\Hello\\Desktop\\xyz.txt”);

BufferedReader br= new BufferedReader(file1);
BufferedWriter bw = new BufferedWriter(file2);
String line;

while ((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
}
bw.close();

br.close();
file2.close();
file1.close();
}
}

Read a Text File (Range of records) and write the data to another file

package package1;

import java.io.*;

public class Sample {

public static void main(String[] args) throws IOException {

FileReader file1 = new FileReader(“C:\\Users\\Hello\\Desktop\\abcd.txt”);
FileWriter file2 = new FileWriter (“C:\\Users\\Hello\\Desktop\\xyz.txt”);

BufferedReader br= new BufferedReader(file1);
BufferedWriter bw = new BufferedWriter(file2);
String line;
int lineCount=0;

while ((line=br.readLine())!=null) {
lineCount=lineCount+1;
if ((lineCount>2) && (lineCount<6)) {
bw.write(line);
bw.newLine();
}
}
bw.close();

br.close();
file2.close();
file1.close();
}
}

 


Java Language Syllabus

Java Videos

Follow me on social media:

2 thoughts on “File Handling in Java”

Comments are closed.