arrays - Can't flush DataOutputStream in Java -
i want put double array datainputstream , print dataoutputstream in console. tried convert byte array first. can't flush() dataoutputstream, gets printed in console. system.out.print(c) works.
double[] b = { 7, 8, 9, 10 }; //double[] byte[] byte[] bytes = new byte[b.length * double.size]; bytebuffer buf = bytebuffer.wrap(bytes); (double d : b) buf.putdouble(d); inputstream = new bytearrayinputstream(bytes); datainputstream dis = new datainputstream(is); dataoutputstream dos = new dataoutputstream(system.out); int c; try{ while( (c = dis.read()) != -1){ //system.out.print(c); dos.writeint(c); } dos.flush(); } catch(exception e){ system.out.println("error: " + e); }
output system.out.print(c), want achieve: 642800000064320000006434000000643600000000000000000000000000000000[...]
writing bytes console may cause control characters( can not printed) , cause unexpected result. if absolutely need see text representation, consider ascii converters such base64.
but in example, replace
dos.writeint(c);
dos.writechars(integer.tostring(n));
and expected result. writeint
writes 4 bytes representing current int , can result in various control characters. writechars
writes sequence of characters instead.
Comments
Post a Comment