ObjectInputStreamã§è¤æ°ã®ãªãã¸ã§ã¯ããã·ãªã¢ã©ã¤ãº
ã£ã¦ãã§ãããã§ãããListã¨ãã®ã³ã¬ã¯ã·ã§ã³ã«çªã£è¾¼ãã§ä¸æ°ã«ã·ãªã¢ã©ã¤ãºããªãã¨ãããªã!ã¨ãåæã«æãè¾¼ãã§ãã¾ããã
import java.io._ // ã·ãªã¢ã©ã¤ãºå¯¾è±¡ class Kitten( name:String, age:int ) extends Serializable { override def toString() = name + " : " + age.toString() } // ã·ãªã¢ã©ã¤ãºã使ããµã³ãã«ã object SerializeSample { def main(args: Array[String]) { // æ¸ã込㿠var b = new ByteArrayOutputStream() var out:ObjectOutputStream = null try { out = new ObjectOutputStream(b) out.writeObject( new Kitten("mii", 1) ) out.writeObject( new Kitten("tora", 2) ) out.writeObject( new Kitten("shiro", 0) ) out.writeObject( new Kitten("kuro", 0) ) } finally { if ( out != null ) { out.close() } } // èªã¿è¾¼ã¿ var in:ObjectInputStream = null try { in = new ObjectInputStream( new ByteArrayInputStream( b.toByteArray() )) println( in.readObject() ) println( in.readObject() ) println( in.readObject() ) println( in.readObject() ) } finally { if ( in != null ) { in.close() } } } }
ãã£ãããªã®ã§Scalaã§ãå®è¡çµæã§ãã
mii : 1 tora : 2 shiro : 0 kuro : 0
ãäºææ§ããªããªãããã¨ãããã使ããªãã¨æãã£ãã·ãªã¢ã©ã¤ãºã§ããã䏿ãã¡ã¤ã«ã¨ããã¼ã¿ç§»è¡ãæããã«ä¸è¦ãªã使ã£ã¦ãããããªã¼ã¨æã仿¥ãã®é ã