java - Object won't read in properly -
i'm writing program writes array object java binary file read in. problem when array read in returning [null, null]. not reading/writing object correctly?
dbms.java
public static void writetofile(datab[] friends) { try { objectoutputstream mystream = new objectoutputstream( new fileoutputstream("datt.dat")); for(int i=0; i<friends.length; i++){ mystream.writeobject(friends[i]); } mystream.close(); } catch (filenotfoundexception e) { } catch (ioexception e) { } system.out.println("\n\narray written file datt."); } /********************************************************** * readfromfile read binary file , return array of objects * calling method catch following exceptions: filenotfoundexception, * classnotfoundexception, , ioexception (know each does!) * * @return array read in binary input file */ public static datab[] readfromfile() { system.out .println("\n\nnow let's reopen file , display array."); datab[] b = null; // name of file open. string filename = "datt.dat"; try { // use reading data. byte[] buffer = new byte[1000]; objectinputstream input = new objectinputstream(new fileinputstream("datt.dat")); input.close(); } catch (filenotfoundexception ex) { system.out.println("unable open file '" + filename + "'"); } catch (ioexception ex) { system.out.println("error reading file '" + filename + "'"); // or this: // ex.printstacktrace(); } return b; }
output: array before writing output file:
[robert , duchar 7345555555, len , gt 7345555567]
array written file datt.
now let's reopen file , display array.
the array read in input file... see array backwards or forwards? enter answer (b or f): f
[null, null]
you declaring b null
value initialization.
datab[] b = null;
then updating value of b
then returning b
initialzed null
Comments
Post a Comment