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 do file handling in Java, what is stream, file handling package, file handling classes and methods, java.io.File.
File Handling in Java
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
Java provides the java.io package for file handling in Java. java.io package contains the inbuilt Java classes and predefined methods using which you can read from and write data to file.
What is Stream
Stream is a sequence of data that can be of two types viz. Byte Stream and Character Stream.
Byte 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
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
- Create a File
- Constructing a File Name path
- Read the File
- Write to a File
- Appending to a File
- Getting the Size of a File
- Count lines of a particular file
- Renaming a File or Directory
- Copying a File to Another File
- Copying Multiple Files
- Moving a File or Directory to Another Directory
- Deleting a File or Directory
- Change a File timestamp
- Create a Temp File
- Delete a Temp File
- Getting an absolute path and Parents of a Filename
- File Handling Tutorials Home
page
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.
java.io.File
0File 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.