File Handling & Serialization In Java

File Handling (I/O handling)

  1. In File handling you can perform read and write operation in file.
  2. To perform file level operations, you can use java build-in class.
  3. All the file classes present inside io package.
  4. In File handling most of the operation throws IOException which is checked exception.

 

File Class

  1. This class is use to perform file level operations like creating, rename, delete file etc.
  2. Also, can use to view file details.
  3. By Using File class, you can also perform the operation on directory/folder level.
  4. FileClasses is present inside java.io package.

 

Files Type In Java

  1. Java distributes file into 2 categories.
    1. Binary Files
      1. The file which has binary/bytes content is a binary file.
      2. Example: images(jpg/png), pdf, doc, zip, exe, mp3, mp4 etc.
    2. Character files
      1. The file which has only chars(ASCII) content is a character file.
      2. Example: .java, .txt, .html, .css, .js
    3. 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.
    4. 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
    5. 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.

 

Serialization

  1. Serialization is a process in which you can write a state/data/variable of an object into file.
  2. 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.
  3. Serialization Steps
    1. Implement Serializable interface on the class for which you want to perform serialization process.
    2. Create Object of FileOutputStream and ObjectOutputStream to write a content inside file.
  4. Deserialization steps
    1. Get the file which is generated by Serialization process.
    2. Create Object of FileInputStream and ObjectInputStream
    3. You also required a java class on which serialization process happened.
  5. 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

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !