在Android上,当一个文件以换行符结束,而另一个文件不以';T
这与我在Android上用Unity保存文本文件,然后用本机Android读取文本文件的情况有关 我们读取的其中一个文件是HMACMD5签名,使用代码创建在Android上,当一个文件以换行符结束,而另一个文件不以';T,android,json,string,unity3d,newline,Android,Json,String,Unity3d,Newline,这与我在Android上用Unity保存文本文件,然后用本机Android读取文本文件的情况有关 我们读取的其中一个文件是HMACMD5签名,使用代码创建 byte[] bData = System.Text.Encoding.UTF8.GetBytes (data); byte[] bKey = System.Text.Encoding.UTF8.GetBytes (key); using (HMACMD5 hmac = new HMACMD5(bKey)) { byte[] sign
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);
}
然后写信给电话
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);
}
}
}
我们保存的另一个字符串是JSON字符串转储。签名在字符串末尾有一个换行符,但JSON字符串没有。我知道我可以手动添加一个,但这个问题是关于读取准确的文件内容
在Android上,根据之前的SO答案,我用
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();
}
我会在每一行之后手动添加换行符,但是如果我这样做,我就无法准确地读取JSON文件,它的末尾没有换行符。如果不添加换行符,则无法准确读取签名文件,而签名文件确实如此。您最好不要使用readLine()而使用read()