Java - ObjectOutputStream.PutField put(String name, long val) method



Description

The Java ObjectOutputStream.PutField put(String name, long val) method puts the value of the named long field into the persistent field.

  • Used in a class's custom writeObject(ObjectOutputStream oos) method.

  • Sets a long field for manual serialization using the field name.

  • You must call oos.writeFields() after put() to flush the field data to the stream.

  • Useful for writing timestamps, IDs, or any long numeric values.

Declaration

Following is the declaration for java.io.ObjectOutputStream.PutField.put(String name, long val) method.

public abstract void put(String name, long val)

Parameters

  • name − The name of the serializable field.

  • val − The value to assign to the field.

Return Value

This method does not return a value.

Exception

  • IllegalArgumentException − If name does not match the name of a serializable field for the class whose fields are being written, or if the type of the named field is not long.

Example - Usage of ObjectOutputStream.PutField put(String name, long val) method

The following example shows the usage of ObjectOutputStream.PutField put(String name, long val) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;

public class ObjectOutputStreamDemo implements Serializable {
   public static void main(String[] args) {
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(new Example());
         oout.flush();
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var of a
         System.out.println("" + a.var);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static public class Example implements Serializable {
      static long var = 76458l;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Long.TYPE)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         var = fields.get("var", 0l);
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable var
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

76458

Example - Serialize a LogEntry with a long timestamp

The following example shows the usage of ObjectOutputStream.PutField put(String name, long val) method. We're writing a long timestamp field using put().

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("log.ser"))) {
         LogEntry entry = new LogEntry("System started");
         oos.writeObject(entry);

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("log.ser"));
         LogEntry le = (LogEntry)ois.readObject();
         System.out.println("LogEntry:: message = '" + le.message +"'; timestamp = " + le.timestamp);
      }
   }

   static class LogEntry implements Serializable {
      private static final long serialVersionUID = 1L;

      String message;
      long timestamp;

      public LogEntry(String message) {
         this.message = message;
         this.timestamp = System.currentTimeMillis();
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("message", message);
         fields.put("timestamp", timestamp); //long field
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         message = (String) fields.get("message", "No message");
         timestamp = fields.get("timestamp", 0L);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

LogEntry:: message = 'System started'; timestamp = 1745388399009

Explanation

  • The timestamp is manually written using put(String, long).

  • During deserialization, a fallback value (0L) is used if it's missing.

Example - Serialize a FileMeta object with a manually assigned fileSize (long)

The following example shows the usage of ObjectOutputStream.PutField put(String name, long val) method. We're writing a file's size using put(), separate from actual file reading.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("filemeta.ser"))) {
         FileMeta meta = new FileMeta("report.pdf", 1048576L); // 1MB
         oos.writeObject(meta);
      }

      // Read it back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("filemeta.ser"))) {
         FileMeta meta = (FileMeta) ois.readObject();
         System.out.println("Deserialized: " + meta.filename + ", size = " + meta.fileSize + " bytes");
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

   static class FileMeta implements Serializable {
      private static final long serialVersionUID = 1L;

      String filename;
      long fileSize;

      public FileMeta(String filename, long fileSize) {
         this.filename = filename;
         this.fileSize = fileSize;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("filename", filename);
         fields.put("fileSize", fileSize);  // long field
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         filename = (String) fields.get("filename", "unknown.txt");
         fileSize = fields.get("fileSize", -1L);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Deserialized: report.pdf, size = 1048576 bytes
java_io_objectoutputstream.putfield.htm
Advertisements