CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
Name of the Experiment: File Handling
Reg No: 20BCE0907
Date: 12th October 2021
Scope:
To obtain the understanding of File Handling in Java by implementing various functions of file
handling
Code:
package javaLab;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.nio.file.*;
public class FileHandlingAssignment {
public static void main(String[] args) {
try {
while(true){
System.out.println("\n\nEnter your Choice:");
System.out.println("1.Create a file");
System.out.println("2.Open and read the file");
System.out.println("3.Write in the file");
System.out.println("4.Copy content from one file to
another");
System.out.println("5.Delete file");
System.out.println("6.Rename file");
System.out.println("7.Help");
System.out.println("8.Exit");
System.out.println("\nEnter your choice here: ");
Scanner obj = new Scanner(System.in);
int ch = obj.nextInt();
if(ch==1){
System.out.println("\nWhat is the name of the file you
want to create?");
Scanner obj1 = new Scanner(System.in);
String choice = obj1.nextLine();
File file = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\" +
choice);
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
boolean created = file.createNewFile();
System.out.println("File " + file.getName() + "
created: " + created);
}
else if(ch==2){
File file = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\");
File[] files = file.listFiles();
System.out.println("\nAvailable .txt files:");
for(File f: files){
if(f.isFile()){
String fname = f.getName();
int lastDot = fname.lastIndexOf(".");
String extension = fname.substring(lastDot+1);
if(extension.equals("txt")){
System.out.println(fname);
}
}
}
System.out.println("\nWhich file do you want to read
from? : ");
Scanner obj2 = new Scanner(System.in);
String fname = obj2.nextLine();
FileInputStream fin = new
FileInputStream("C:\\Users\\dhruv\\IdeaProjects\\Java Lab
Programs\\src\\javaLab\\" + fname);
System.out.println("Data in the file " + fname + ":");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}
else if(ch==3){
File file = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\");
File[] files = file.listFiles();
System.out.println("\nAvailable .txt files:");
for(File f: files){
if(f.isFile()){
String fname = f.getName();
int lastDot = fname.lastIndexOf(".");
String extension = fname.substring(lastDot+1);
if(extension.equals("txt")){
System.out.println(fname);
}
}
}
System.out.println("\nWhich file do you want to write
into? : ");
Scanner obj1 = new Scanner(System.in);
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
String fname = obj1.nextLine();
System.out.println("Enter the string: ");
Scanner obj3 = new Scanner(System.in);
String s = obj3.nextLine();
byte b[] = s.getBytes();
FileOutputStream fout = new
FileOutputStream("C:\\Users\\dhruv\\IdeaProjects\\Java Lab
Programs\\src\\javaLab\\" + fname);
fout.write(b);
fout.close();
System.out.println("Data successfully written into the
file");
}
else if(ch==4){
File file = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\");
File[] files = file.listFiles();
System.out.println("\nAvailable .txt files:");
for(File f: files){
if(f.isFile()){
String fname = f.getName();
int lastDot = fname.lastIndexOf(".");
String extension = fname.substring(lastDot+1);
if(extension.equals("txt")){
System.out.println(fname);
}
}
}
Scanner obj1 = new Scanner(System.in);
System.out.print("\nOriginal file: ");
String origin = obj1.nextLine();
Scanner obj2 = new Scanner(System.in);
System.out.print("Destination file: ");
String dest = obj2.nextLine();
FileInputStream fin = new
FileInputStream("C:\\Users\\dhruv\\IdeaProjects\\Java Lab
Programs\\src\\javaLab\\" + origin);
FileOutputStream fout = new
FileOutputStream("C:\\Users\\dhruv\\IdeaProjects\\Java Lab
Programs\\src\\javaLab\\" + dest);
int i;
while ((i = fin.read()) != -1) {
fout.write(i);
}
System.out.println("Data copied successfully!");
fin.close();
fout.close();
}
else if(ch==5) {
File file = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\");
File[] files = file.listFiles();
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
System.out.println("\nAvailable .txt files:");
for (File f : files) {
if (f.isFile()) {
String fname = f.getName();
int lastDot = fname.lastIndexOf(".");
String extension = fname.substring(lastDot +
1);
if (extension.equals("txt")) {
System.out.println(fname);
}
}
}
System.out.println("\nWhich file do you want to
delete?");
Scanner obj1 = new Scanner(System.in);
String fname = obj1.nextLine();
Files.deleteIfExists(Paths.get("C:\\Users\\dhruv\\IdeaProjects\\Java Lab
Programs\\src\\javaLab\\" + fname));
System.out.println("File Deleted!");
}
else if(ch==6){
File file = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\");
File[] files = file.listFiles();
System.out.println("\nAvailable .txt files:");
for(File f: files){
if(f.isFile()){
String fname = f.getName();
int lastDot = fname.lastIndexOf(".");
String extension = fname.substring(lastDot+1);
if(extension.equals("txt")){
System.out.println(fname);
}
}
}
System.out.print("Name of the file you want to rename:
");
Scanner obj1 = new Scanner(System.in);
String fname = obj1.nextLine();
File frename = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\" +
fname);
System.out.print("New name: ");
Scanner obj2 = new Scanner(System.in);
String frname = obj2.nextLine();
File newName = new
File("C:\\Users\\dhruv\\IdeaProjects\\Java Lab Programs\\src\\javaLab\\" +
frname);
boolean renameStatus = frename.renameTo(newName);
System.out.println("Rename Successful: " +
renameStatus);
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
else if(ch==7){
System.out.println("\nHello! Welcome to My Text
Editor!!\n" +
"Here you will be able to Create, Edit, Delete,
Rename, etc. your files by using the options in the menu.\n" +
"You can choose the options and perform
different functions on files\n" +
"\t\t\tThank You!!!");
}
else if(ch==8){
System.exit(0);
}
}
} catch(IOException exception) {
System.out.println("An unexpected error is occurred.");
exception.printStackTrace();
}
}
}
Output:
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
I forgot to run help in the above screenshots. So I’ve run 7.Help and pasted the screenshot below.
Knowledge gained:
Learnt to use different file handling functions and obtained the knowledge how how and where to
use these functions
CSE1007 JAVA Programming lab
Faculty: Dr.Manikandan N
Application Area
Can be used to handle and manipulate files and their data