File Handling
When working with Java, handling files is a very common task — whether you're storing data, reading input, or creating logs.
Java provides several classes in the java.io package to make file handling easy. Let’s understand the most important ones in a simple way.
1. File Class – Working with Files & Folders
The File class is used to create, delete, and get details about files or directories.
Important:
It does NOT read or write data inside the file — it only manages file properties.
Common Methods
createNewFile() → Create a new file
exists() → Check if file exists
getName() → Get file name
getAbsolutePath() → Get full file path
canRead() → Check read permission
canWrite() → Check write permission
canExecute() → Check execute permission
length() → Get file size
delete() → Delete file
mkdir() → Create folder
mkdirs() → Create multiple folders
list() → List files in directory
2. FileReader – Reading Text Files
FileReader is used to read characters from a file.
It’s a simple class, mainly used for basic file reading.
Common Methods
read() → Read one character
read(char[]) → Read multiple characters
read(char[], off, len) → Read with position
ready() → Check if ready to read
skip(n) → Skip characters
getEncoding() → Get encoding type
close() → Close file
Best for small text files.
**
- FileWriter – Writing to Files**
FileWriter is the easiest way to write text into a file.
If the file already exists, it will usually overwrite the content.
Common Methods
write(int) → Write single character
write(char[]) → Write character array
write(String) → Write string
write(String, off, len) → Write part of string
append() → Add content at end
flush() → Force write immediately
close() → Close file
Best for simple writing tasks.
**
- BufferedReader – Fast Reading**
BufferedReader makes reading faster and more efficient by using a buffer.
Instead of reading character-by-character, it reads large chunks at once.
Common Methods
read() → Read one character
readLine() → Read full line
read(char[], off, len) → Read into array
ready() → Check readiness
skip(n) → Skip characters
mark() → Mark current position
reset() → Go back to marked position
close() → Close stream
Best for reading large files efficiently.
5. BufferedWriter – Fast Writing
BufferedWriter improves writing performance by reducing file access operations.
Common Methods
write(int) → Write one character
write(char[]) → Write character array
write(String) → Write string
write(String, off, len) → Write part of string
newLine() → Add a new line
flush() → Force write
close() → Close stream
Best for writing large data efficiently.
**
- PrintWriter – Formatted Output**
PrintWriter is used when you want clean and formatted output.
It works similar to System.out.print() but writes into files.
Common Methods
print() → Write without newline
println() → Write with newline
printf() → Formatted output
format() → Same as printf
write() → Write raw data
append() → Add data
flush() → Force write
close() → Close writer
Top comments (0)