Oopex 9
Oopex 9
9 FILE HANDLING
18/10//24 URK23AI1035
Aim :
To design and implement a comprehensive Java program that effectively utilizes multithreading
concepts to manage, process, and analyze large datasets containing record information, thereby
improving overall performance and efficiency
Description :
File handling in Java is accomplished through a set of classes and interfaces
provided in the java.io and java.nio.file packages. These tools enable
developers to interact with the file system, perform operations such as creating,
reading, writing, renaming, and deleting files and directories, as well as managing file
permissions and attributes.
Key Components of File Handling in Java:
1. File Class:
o Represents a file or directory path in the file system.
o Provides methods for creating, deleting, renaming files and directories, and
checking file properties (like size, read/write permissions).
2. FileInputStream and FileOutputStream:
o Used for reading from and writing to files in binary format.
o Suitable for handling binary data like images, audio files, etc.
3. FileReader and FileWriter:
o Used for reading from and writing to files in character format.
o Ideal for text files and handles character encoding.
4. java.nio.file package:
o Introduces the Path and Files classes for more advanced file operations and better
handling of file paths.
82
• Deleting Files: Removing files from the file system.
• Listing Directories: Retrieving and displaying the names of files within a directory.
1. Create a file named MyProfile.txt. Write your own profile into the file, MyProfile.txt.
Copy this file into another location and delete the file, MyProfile.txt.
Program :
import java.io.*;
import java.nio.file.*;
} catch (IOException e) {
e.printStackTrace();
try {
83
Files.createDirectories(Paths.get("backup")); // Create the backup directory if it
doesn't exist
Files.copy(Paths.get(fileName), Paths.get(copyLocation),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
try {
Files.delete(Paths.get(fileName));
} catch (IOException e) {
e.printStackTrace();
OUTPUT:
84
2. Perform the following operations on file using menu-driven application:
➢ Opening a existing file
➢ Creating a new file
➢ Renaming a file
➢ Deleting a file
➢ Creating a directory
➢ Finding the absolute path of a file
➢ Get the file names of a directory
Program :
OUTPUT :
package exercise9;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
System.out.println("6. Find the absolute path of a file"); System.out.println("7. Get the file
names of a directory"); System.out.println("8. Exit");
try {
}
85
fileReader.close();
try {
if (file.createNewFile()) {
} else {
public static void renameFile(String oldName, String newName) { File oldFile = new
File(oldName);
if (oldFile.renameTo(newFile)) {
} else {
}
86
public static void deleteFile(String filename) { File file = new File(filename);
if (file.delete()) {
} else {
if (directory.mkdir()) {
} else {
try {
} catch (Exception e) {
87
String[] files = directory.list();
if (files != null) {
} else {
while (true) {
displayMenu();
switch (choice) {
case 1:
break; case 2:
break; case 3:
88
System.out.print("Enter the current filename: "); String oldName = scanner.nextLine();
System.out.print("Enter the new filename: "); String newName = scanner.nextLine();
renameFile(oldName, newName);
break; case 4:
break; case 5:
break; case 6:
System.out.print("Enter the filename to find the absolute path: "); String absPathFilename =
scanner.nextLine(); findAbsolutePath(absPathFilename);
break; case 7:
break; case 8:
System.out.println("Exiting..."); scanner.close();
return; default:
89
RESULT :
The program for File Handling has been completed and output is verified successfully.
90