DEV Community

Vidya
Vidya

Posted on

File Handling in Java and Important Methods

File Handling in Java
File handling in Java is the process of creating, reading, writing, and deleting files using Java programs. It allows applications to store data permanently instead of keeping it only in memory.
Java provides built-in classes in the
--> java.io (Input/Output) package to perform file operations

Why File Handling is Important?
* Store data permanently
* Read and update data anytime
* Useful for logs, reports, configs
* Helps in real-world applications (banking, apps, etc.)

Important Methods in Java File Handling
1.next() → Reads word
2.exists() → Checks if file exists
3.delete() → Deletes file
4.getName() → Returns file name
5.getAbsolutePath() → Returns full path
6.length() → Returns file size
7.write(String) → Writes text
8.append(String) → Adds text
9.close() → Closes file
10.read() → Reads character
11.readLine() → Reads line by line
12.hasNextLine() → Checks next line
13.nextLine() → Reads line
14.canRead() → Checks if file is readable
15.canWrite() → Checks if file is writable
16.canExecute() → Checks execution permission
17.isFile() → Checks if it is a file
18.isDirectory() → Checks if it is a folder
19.mkdir() → Creates a directory
20.mkdirs() → Creates multiple directories
21.list() → Lists file names in directory
22.listFiles() → Returns file objects in directory
23.renameTo(File newFile) → Renames file
24.flush() → Clears buffer
25.print() → Prints text
26.createNewFile() → Creates a new file

Top comments (0)