java - Saving large short array android -
i'm looking way save , reload few large arrays (about 5 million short
) in fastish way on android. app needs save them in way them later, can't hold them in memory...
so far, i've tried converting them byte[]
array , seem save successfully, can't data back, how saving code works (it's separate functions, simplified here):
bytearrayoutputstream baos = new bytearrayoutputstream(); objectouputstream oos = new objectoutputstream(baos); oos.writeobject(data); oos.close(); baos.close(); fileoutputstream fos = new fileoutputstream(filename); // valid absolute path fos.write(baos.tobytearray()); fos.close();
and loading part stuck, how short[]
byte[]
? read database might work too, quick enough?
i've looked around stackoverflow , google , can't seem find similar problem, or @ least solution it, beginner might have missed obvious...
if data
short[]
, want write whole thing file, don't use buffer in memory, write directly.
objectoutputstream oos = new objectoutputstream(new fileoutputstream(filename)); try { oos.writeobject(data); } { oos.close(); }
if write via objectoutputstream
, have read via objectinputstream
since it's not writing pure data type information. if put short[]
in, short[]
(you try skip bytes have analyze stream writing). applies if objectoutputstream
writes bytearrayoutputstream
.
if don't want handle mess, objectoutputstream
does:
dataoutputstream dos = new dataoutputstream(new fileoutputstream(filename)); try { (int = 0; < data.length; i++) { dos.writeshort(data[i]); } } { dos.close(); }
dataoutputstream
writes plain data can read data directly byte[]
if want. if byte order matters: it's using big-endian.
since approach writes single bytes instead of chucks of byte[]
benefits using bufferedoutputstream
in between. default uses 8kb buffer, can increased give better results. writing data convert short
byte
s in memory , once enough data available whole chunk pushed low level os functions write it.
int buffersize = 32 * 1024; dataoutputstream dos = new dataoutputstream( new bufferedoutputstream( new fileoutputstream(filename), buffersize) ); try { (int = 0; < data.length; i++) { dos.writeshort(data[i]); } } { dos.close(); }
Comments
Post a Comment