File Handling (I/O handling)
- In File handling you can perform read and write operation in file.
- To perform file level operations, you can use java build-in class.
- All the file classes present inside io package.
- In File handling most of the operation throws IOException which is checked exception.
- This class is use to perform file level operations like creating, rename, delete file etc.
- Also, can use to view file details.
- By Using File class, you can also perform the operation on directory/folder level.
- FileClasses is present inside java.io package.
Files Type In Java
- Java distributes file into 2 categories.
- Binary Files
- The file which has binary/bytes content is a binary file.
- Example: images(jpg/png), pdf, doc, zip, exe, mp3, mp4 etc.
- Character files
- The file which has only chars(ASCII) content is a character file.
- Example: .java, .txt, .html, .css, .js
- To perform read and write operation on binary and Character type of file, we can use InputStream and OutputStream implemented classes. The mostly used implemented classes are FileInputStream and FileOutputStream.
- To perform read and write operation on character file, we can use Reader and Writer implemented classes. There are multiple implemented classes of Reader and Writer interface. Mostly used classes are FileReader and PrintWriter is also used to perform write operation inside file
- FileWriter: Is use to write content inside file. FileWriter will create a file if not exist, if file is exist then by default it overrides the content of the file.
- Binary Files
- Serialization is a process in which you can write a state/data/variable of an object into file.
- By using this process, you can store java object into file, and send across the network to another user, who can de-serialize this object and get it into a java state.
- Serialization Steps
- Implement Serializable interface on the class for which you want to perform serialization process.
- Create Object of FileOutputStream and ObjectOutputStream to write a content inside file.
- Deserialization steps
- Get the file which is generated by Serialization process.
- Create Object of FileInputStream and ObjectInputStream
- You also required a java class on which serialization process happened.
- Serial version id is use to make sure that serialization and deserialization is happening with same id. If de-serialization performs with another serial id then you will get and exception.
Join Telegram : Click Here