Java by API/org.apache.commons.lang/SerializationUtils — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:47, 31 мая 2010

SerializationUtils: deserialize(InputStream in)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.rumons.lang.SerializationUtils;
public class SerializationUtilsTrial {
  public static void main(String[] args) {
    try {
      // File to serialize object to
      String fileName = "testSerialization.ser";
      // New file output stream for the file
      FileOutputStream fos = new FileOutputStream(fileName);
      // Serialize String
      SerializationUtils.serialize("SERIALIZE THIS", fos);
      fos.close();
      // Open FileInputStream to the file
      FileInputStream fis = new FileInputStream(fileName);
      // Deserialize and cast into String
      String ser = (String) SerializationUtils.deserialize(fis);
      System.out.println(ser);
      fis.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





SerializationUtils.serialize(Serializable arg0, OutputStream arg1)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.rumons.lang.SerializationUtils;
public class SerializationUtilsTrial {
  public static void main(String[] args) {
    try {
      // File to serialize object to
      String fileName = "testSerialization.ser";
      // New file output stream for the file
      FileOutputStream fos = new FileOutputStream(fileName);
      // Serialize String
      SerializationUtils.serialize("SERIALIZE THIS", fos);
      fos.close();
      // Open FileInputStream to the file
      FileInputStream fis = new FileInputStream(fileName);
      // Deserialize and cast into String
      String ser = (String) SerializationUtils.deserialize(fis);
      System.out.println(ser);
      fis.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}