In Java , File handling involves performing operations like creating , reading , writing and deleting files using classes from the java.io and java.nio.file packages . This java file handling process helps a program to save and use information permanently on the computer.
Why File Handling is used?
- To store data permanently instead of keeping it only in the memory.
- To read data from files or write data from files for later use .
- To share the data between different programs or systems
- To organize and manage large data efficiently.
To support file handling, Java provides the File class in the java.io package.
File Class:
File class in Java is used to represent the name and path of a file or directory . File class provides methods to create, delete and get information about the files and directories.
In Java , I/O streams are used to perform input and output operations on files .
I/O Stream in Java:
In Java , I/O streams are the fundamental mechanism for handling input and output operations . I/O streams provides a uniform way to read data from various sources (files ,network,memory ) and write data to different into sources.
Java I/O streams are categorized into two main types based on the type of data they handle:
- Byte Streams : In Java, Byte Streams are used to handle raw binary data such as image, files , audio ,videos or any non-text file .
The two main abstract classes for byte stream are :
- InputStream : For reading data
- OutputStream : For writing data
As the abstract classes cannot be used directly , you can use their implementation classes to perform actual I/O operations .
- FileInputStream : This class reads raw bytes from a file
- FileOutputStream : This class writes raw bytes to a file.
- BufferedInputStream/BufferedOutputStream : These classes use buffering for faster performance .
- ByteArrayInputStream: This class reads data from a byte array as if it were an input stream.
- ByteArrayOutputStream : This class writes data into a byte array , which grows automatically.
2. Character Streams :
In Java , Character Streams are used to handle text data. Character Streams work with 16-bit Unicode characters , which make them suitable for international text and language support.
Two main abstract classes for character streams are :
- Reader : This class is the base class for all character based input streams(for reading).
- Writer : This class is the base class for all character based output streams (writing)
As abstract class cannot be used directly, you can use their implementation classes to perform actual I/O operations .
- FileReader: This class reads characters from a file .
- FileWriter : Writes character to a file.
- BufferedReader: reads text efficiently using buffering. This class provides readLine() method for reading lines.
- BufferedWriter: writes text efficiently using buffering .
- StringReader: reads character from a string.
- StringWriter: writes characters into a string buffer.
Common File Operations and Examples :
- Create a File : Use file.createNewFile() to generate a new file.
- Write to a File : Use FileWriter or BufferedWriter for text and FileOutputStream for binary data.
- Read from a File : Use BufferredReader with FileReader for text or Scanner for easier parsing.
- Delete a File : Call file.delete() to remove an existing file.
- Get File Information: Use file.exists() , file.getName() and file.length() to check file details .
