0% found this document useful (0 votes)
27 views10 pages

File Handling

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

File Handling

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

File Handling in Java

Introduction
• File handling in Java refers to the process of working with files,
such as creating, reading, writing, and manipulating data within
files.

• Java provides a set of classes and methods in the java.io package


to perform file handling operations.
Why file handling is required?
• Enables us to store the output of any particular program in a file and
allows us to perform certain operations on it.

• Data Sharing: Files facilitate the sharing of data between different


programs or between different instances of the same program.

• Configuration and Settings: Many applications use files to store


configuration settings and preferences. This allows users to customize the
behavior of an application.
File operations in Java

The following are the several operations that can be performed


on a file in Java ,
• Create a File
• Read from a File
• Write to a File
• Delete a File
Create a file
• To create a file in Java, you can use
the createNewFile() method.

• This method returns a boolean value: true if the file was


successfully created, and false if the file already exists. Note
that the method is enclosed in a try...catch block.

• This is necessary because it throws an IOException if an error


occurs (if the file cannot be created for some reason):
import java.io.File; // Import the File class
import java.io.IOException; // Import the IOException class to
handle errors
public class CreateFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
}
}
}
To create a file in a specific directory

ile myObj = new File("C:\\Users\\MyName\\filename.txt");


Write to a file
import java.io.FileWriter; // Import the FileWriter class
import java.io.IOException; // Import the IOException class to handle errors

public class WriteToFile {


public static void main(String[] args) {
try {
Use the FileWriter class together
FileWriter myWriter = new FileWriter("filename.txt");
with its write() method to write some
myWriter.write("This file contains important information"); text to the file
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
//e.printStackTrace();
}
}
}
Read a file
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {


public static void main(String[] args) {
try {
File myObj = new File("filename.txt"); Use the Scanner class to read the
Scanner myReader = new Scanner(myObj); contents of the text file created
while (myReader.hasNextLine()) { previously
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
//e.printStackTrace();
}
}
Delete a file
import java.io.File; // Import the File class

public class DeleteFile {


public static void main(String[] args) { To delete a file in Java,
File myObj = new File("filename.txt"); use the delete() method:
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy