0% found this document useful (0 votes)
26 views6 pages

File Handling in Java

File handling in Java allows programs to read and write data permanently using classes from the java.io and java.nio packages. Key classes include File, FileWriter, FileReader, and Scanner, and it is important to handle exceptions and close files after use to prevent resource leaks. Example programs demonstrate writing to and reading from files.

Uploaded by

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

File Handling in Java

File handling in Java allows programs to read and write data permanently using classes from the java.io and java.nio packages. Key classes include File, FileWriter, FileReader, and Scanner, and it is important to handle exceptions and close files after use to prevent resource leaks. Example programs demonstrate writing to and reading from files.

Uploaded by

jose2985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

File Handling in Java

Understanding file operations with


example programs
Introduction to File Handling
• • File handling allows programs to read/write
data permanently.
• • It is part of [Link] and [Link] packages.
• • Common classes used:
• - File
• - FileReader / FileWriter
• - BufferedReader / BufferedWriter
• - FileInputStream / FileOutputStream
• - Scanner
The File Class
• • The File class represents files or directories.
• • It provides methods to create, delete, and
get file information.

• Example:
• File obj = new File("[Link]");
• if([Link]()) {
• [Link]("File exists");
• } else {
Writing to a File Example
• import [Link];
• import [Link];

• public class WriteToFile {


• public static void main(String[] args) {
• try {
• FileWriter writer = new
FileWriter("[Link]");
• [Link]("Hello, File Handling in
Reading from a File Example
• import [Link];
• import [Link];
• import [Link];

• public class ReadFromFile {


• public static void main(String[] args) {
• try {
• File file = new File("[Link]");
• Scanner reader = new Scanner(file);
Summary
• • File handling enables persistent data
storage.
• • Key classes: File, FileWriter, FileReader,
Scanner.
• • Always handle exceptions like IOException.
• • Close files after use to avoid resource leaks.

You might also like