json - On Android, how to read the exact text of a file, when one finishes with a newline, and another doesn't -
this related situation find myself in working saving text files in unity on android, reading them in native android.
one of files read hmacmd5 signature, created code,
byte[] bdata = system.text.encoding.utf8.getbytes (data); byte[] bkey = system.text.encoding.utf8.getbytes (key); using (hmacmd5 hmac = new hmacmd5(bkey)) { byte[] signature = hmac.computehash (bdata); return system.convert.tobase64string (signature); }
and written phone with,
public static void savetext (string path, string data) { using (filestream fs = new filestream(path, filemode.create)) { using (streamwriter sw = new streamwriter(fs)) { sw.write (data); } } }
the other string we're saving json string dump. signature has newline character @ end of string, json string doesn't. know can manually add one, question reading accurate file contents.
on android, based on previous answers, read file with,
string readfile(file file) { stringbuilder text = new stringbuilder(); try { bufferedreader br = new bufferedreader(new filereader(file)); string line; while ((line = br.readline()) != null) { text.append(line); text.append("\n"); } br.close(); } catch (ioexception e) { mylogger.e(log_tag, "error opening file " + file.getpath(), e); } return text.tostring(); }
i'm manually adding newline character after every line, if this, don't accurately read json file, doesn't have newline character @ end. if don't add newline, don't accurately read signature file, does.
you better not use readline() read().
Comments
Post a Comment