Save non-pickleable variable?

Israel Brewster israel at ravnalaska.net
Fri Oct 20 16:19:09 EDT 2017


On Oct 20, 2017, at 11:09 AM, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> 
> Israel Brewster <israel at ravnalaska.net> writes:
>> Given that, is there any way I can write out the "raw" binary
>> data to a file
> 
>  If you can call into the Java SE library, you can try
> 
> docs.oracle.com/javase/9/docs/api/java/io/ObjectOutputStream.html#writeObject-java.lang.Object-
> 
>  , e.g.:
> 
> public static void save
> ( final java.lang.String path, final java.lang.Object object )
> { try
>  { final java.io.FileOutputStream fileOutputStream
>    = new java.io.FileOutputStream( path );
> 
>    final java.io.ObjectOutputStream objectOutputStream
>    = new java.io.ObjectOutputStream( fileOutputStream );
> 
>    objectOutputStream.writeObject( object );
> 
>    objectOutputStream.close(); }
> 
>  catch( final java.io.IOException iOException )
>  { /* application-specific code */ }}
> 
> 
>>             , and read it back in later?
> 
>  There's a corresponding »readObject« method in
>  »java.io.ObjectInputStream«. E.g.,
> 
> public static java.lang.Object load( final java.lang.String path )
> {
>  java.io.FileInputStream fileInputStream = null;
> 
>  java.io.ObjectInputStream objectInputStream = null;
> 
>  java.lang.Object object = null;
> 
>  try
>  { fileInputStream = new java.io.FileInputStream( path );
> 
>    objectInputStream = new java.io.ObjectInputStream
>    ( fileInputStream );
> 
>    object = objectInputStream.readObject();
> 
>    objectInputStream.close(); }
> 
>  catch( final java.io.IOException iOException )
>  { java.lang.System.out.println( iOException ); }
> 
>  catch
>  ( final java.lang.ClassNotFoundException classNotFoundException )
>  { java.lang.System.out.println( classNotFoundException ); }
> 
>  return object; }
> 
>  However, it is possible that not all objects can be
>  meaningfully saved and restored in that way.

Thanks for the information. In addition to what you suggested, it may be possible that the Java library itself has methods for saving this object - I seem to recall the methods for displaying the data having options to read from files (rather than from the Java object directly like I'm doing), and it wouldn't make sense to load from a file unless you could first create said file by some method. I'll investigate solutions java-side.

-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------

> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list