java - Read byte values contained in file into byte array -
if have file contains exact byte[] info want:
[-119, 45, 67, ...] how can read values byte array? have read them string, , convert string values byte values?
this i'm doing, , works, slowly:
bufferedreader reader = null; string mline = null; assetmanager assetmanager = customapplication.getcustomappcontext().getassets(); seek { reader = new bufferedreader(new inputstreamreader(assetmanager.open(filename))); mline = reader.readline(); } grab (ioexception e) { e.printstacktrace(); } { if (reader != null) { seek { reader.close(); } grab (ioexception e) { e.printstacktrace(); } } } string[] bytevalues = mline.substring(1, mline.length() - 1).split(","); byte[] imagebytes = new byte[bytevalues.length]; (int = 0; < imagebytes.length; i++) { imagebytes[i] = byte.valueof(bytevalues[i].trim()); } homecoming imagebytes; update: there seems misunderstanding of want accomplish. file contains exact byte info want in final byte array. example, want convert file exactly
[-119, 45, 67, ...] into byte[] called result with
result[0] = -119 result[1] = 45 result[2] = 67
bufferedreader used read char units. utilize bufferedinputstream reads byte units , utilize bytearrayoutputstream help filling byte array:
bufferedinputstream bis = new bufferedinputstream(assetmanager.open(filename)); bytearrayoutputstream buffer = new bytearrayoutputstream(); int thebyte; while((thebyte = bis.read()) != -1) { buffer.write(thebyte); } bis.close(); buffer.close(); byte[] result = buffer.tobytearray(); java android bytearray
No comments:
Post a Comment