File – Managing Files
The java.io.File class is used to represent file and directory pathnames in an abstract manner. It doesn't represent the contents of a file but allows you to create, delete, check properties (like size, existence), and manipulate files or directories
| Method | Purpose |
|---|---|
createNewFile() |
Create file |
exists() |
Check file exists |
getName() |
Get file name |
getAbsolutePath() |
Get file path |
canRead() |
Check read permission |
canWrite() |
Check write permission |
canExecute() |
Check execute permission |
length() |
File size |
delete() |
Delete file |
mkdir() |
Create directory |
mkdirs() |
Create multiple directories |
list() |
List files |
FileReader – Reading Characters
Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.
| Method | Purpose |
|---|---|
read() |
Read one character |
read(char[]) |
Read multiple characters |
read(char[], off, len) |
Read with position |
ready() |
Check readiness |
skip(n) |
Skip characters |
getEncoding() |
Get encoding |
close() |
Close file |
FileWriter – Writing Data
- If you are just starting with Java, the easiest way to write text to a file is by using the FileWriter class.
- In the example below, we use FileWriter together with its write() method to create and write some text into a file.
| Method | Purpose |
|---|---|
write(int) |
Write one character |
write(char[]) |
Write character array |
write(String) |
Write string |
write(String, off, len) |
Write part of string |
append() |
Add data at end |
flush() |
Force write |
close() |
Close file |
BufferedReader – Efficient Reading
- The BufferedReader class in Java helps read text efficiently from files or user input. It stores data in a buffer, making reading faster and smoother instead of reading one character at a time.
- Faster Reading Reads large chunks of data at once, reducing the number of read operations.
| Method | Purpose |
|---|---|
read() |
Read one character |
readLine() |
Read full line |
read(char[], off, len) |
Read into array |
ready() |
Check readiness |
skip(n) |
Skip characters |
mark() |
Mark position |
reset() |
Return to mark |
close() |
Close stream |
BufferedWriter – Efficient Writing
The BufferedWriter class is used to write text to a file, one line or one string at a time. If the file already exists, its contents will be replaced (overwritten).
| Method | Purpose |
|---|---|
write(int) |
Write one character |
write(char[]) |
Write character array |
write(String) |
Write string |
write(String, off, len) |
Write part of string |
newLine() |
Add new line |
flush() |
Force write |
close() |
Close stream |
PrintWriter – Formatted Writing
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character
| Method | Purpose |
|---|---|
print() |
Write without new line |
println() |
Write with new line |
printf() |
Formatted output |
format() |
Same as printf |
write() |
Write raw data |
append() |
Add data |
flush() |
Force write |
close() |
Close writer |
Top comments (0)