What is File Handling?
$ File Handling is the process of creating, reading, writing, updating, and deleting files using a programming language — in this case, Java.
$ It allows a Java program to interact with files stored on your computer or server.
Why File Handling is Important:
$ To store data permanently (like user data, logs, reports).
$ To read data that was saved earlier.
$ To modify existing files (update, append).
$ To automate file-related tasks (like backup, reports, etc.).
In Java, File Handling is done using classes from:
$ java.io package (traditional way)
$ java.nio.file package (newer, more efficient way)
File class:
$ the file is class alredy store in package.
$ File is a class used file path in object.
$ invok in object.
$ file object print (mostly path or refrence).
File file = new File("E:\project/sugu.txt");
file.createNewFile();
System.out.println(file);
canExecute():
$ can this file excute it.it returns a boolean.
System.out.println(file.canExecute());
canRead():
$ //can we read file.
System.out.println(file.canRead());
canWrite():
$ can we write the file
canWrite():
$ we can wtite the file.
System.out.println(file.canWrite());
delete():
$ we can delete the file.
System.out.println(file.delete());
exists():
$ it returns whether the file really exists or not.
System.out.println(file.exists());
getAbsolutePath():#
$ computer full path file current location.
getAbsolutePath():# System.out.println(file.getAbsolutePath());
getPath:
$ this file return create give the path
System.out.println(file.getPath());
getName:
$ this file return only name(sugu.txt).
System.out.println(file.getName());
isDirectory:
$ Directory is folder.
file.isFile():
$ should be file.
System.err.println(file.isFile());
Top comments (0)