0% found this document useful (0 votes)
316 views8 pages

File Handling in Java

The document discusses file handling in Java using the File class. The File class allows working with files and has methods like canRead(), canWrite(), createNewFile(), delete(), and more. Examples are provided to demonstrate how to create a file, write text to a file using FileWriter, read a file using Scanner, and delete a file.

Uploaded by

Dipendra Km
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
316 views8 pages

File Handling in Java

The document discusses file handling in Java using the File class. The File class allows working with files and has methods like canRead(), canWrite(), createNewFile(), delete(), and more. Examples are provided to demonstrate how to create a file, write text to a file using FileWriter, read a file using Scanner, and delete a file.

Uploaded by

Dipendra Km
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 8

FILE HANDLING

In java
Introduction
The File class from the java.io package, allows us to work with files.

To use the File class, create an object of the class, and specify the filename or
directory name:

import java.io.File; // Import the File class

File myObj = new File("filename.txt"); // Specify the filename


The File class has many useful methods for creating and getting information about
files. For example:

Method Type Description

canRead() Boolean Tests whether the file is readable or not

canWrite() Boolean Tests whether the file is writable or not

createNewFile() Boolean Creates an empty file

delete() Boolean Deletes a file

exists() Boolean Tests whether the file exists

getName() String Returns the name of the file

getAbsolutePath() String Returns the absolute pathname of the file

length() Long Returns the size of the file in bytes

list() String[] Returns an array of the files in the directory

mkdir() Boolean Creates a directory


Create a File
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.");
e.printStackTrace();
}
}
}
Write To a File
we use the FileWriter class together with its write() method to write some text to the file we
created in the example above. Note that when you are done writing to the file, you should
close it with the close() method:
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 {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Read a File

we use the Scanner class to read the contents of the text 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");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
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) {
File myObj = new File("filename.txt");
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}

You might also like