In this section we will discuss about the file handling in Java.
This section describes you about how to perform file handling in Java. In this section you will read what is file handling in Java, how to perform file handling in Java, what is stream, file handling package, file handling classes and methods, java.io.File.
What is File Handling in Java
File handling in Java means that how to read from and write to file in Java. Java provides the basic I/O package for reading and writing streams. java.io package allows to do all Input and Output tasks in Java .
How to perform file handling in Java
What is Stream
Stream is a sequence of data that can be of two types viz. Byte Stream and Character Stream.
Byte Stream is a stream which incorporates with byte data. When an input and output process happens with byte data then it is called the file handling process with byte stream.
Character Stream is a stream which incorporates with characters. When an input and output process happens with character then it is called the file handling process with byte stream.
File Handling Package
Basically java.io package is used for file handling.
File Handling classes and methods
For file handling following classes and methods are useful :
Byte Stream classes are as follows :
InputStream, OutputStream, FileInputStream, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream, PrintStream, RandomAccessfile etc.
Methods defined by InputStream :
read(), read(byte r), available(), close(), etc.
Methods defined by OutputStream :
write(int w), write(byte w), finalize(), close(), etc.
Here are examples of File Handling in Java Programming Language
Character Stream classes are as follows :
Reader, Writer. These abstract classes are defined for reading and writing character streams. These classes are placed at the top of the class hierarchy.
Methods defined by Reader :
read(), read(char buffer), read(char buffer, int offset, int numChars), ready(), reset() etc.
Methods defined by Writer :
append(char ch), append(CharSequence chars), write(int ch), write(char buffer), write(String str), write(String str, int offset, int numChars), etc.
File class in java.io package contains the information about a file. An efficient and effective class for manipulating a file, or computer's file system. You can use the following methods of File for manipulating with a file.
getName(), getParent(), getParentFile(), getPath(), getAbsolutePath(), isDirectory(), isFile(), createNewFile(), delete(), mkdir(), mkdirs(), etc are the methods of File class that can be used for manipulating a file.