Android,读取存储的String字节数组时,returns个不同的字节数组值
Android, When reading stored String byte array, it returns different byte array values
我正在尝试将字符串字节存储到一个文本文件中,它在我的电脑上运行良好,但是当我尝试将它实现到我的 android 项目中时,它存储了它,但是当我想取回字符串字节时并将其转换为它不起作用的原始字节。它再次在我的电脑上运行,我不知道为什么它在我的 android 项目上不起作用。
代码:
try {
String y = "Yyyyyyy";
try {
File file1 = new File(Environment.getExternalStorageDirectory() + File.separator + "test/test/newFile.txt");
if (!file1.createNewFile()) {
EncryptedObject a = encryptedMessage.encrypt(y, "test", "test");
Log.e("BYTES FROM TEXT1", Arrays.toString(a.getEncryptedBytes()));
String example = new String(a.getEncryptedBytes());
Log.e("STRINGGGGG", example);
BufferedWriter buffer = new BufferedWriter(new FileWriter(file1));
buffer.write(example);
buffer.flush();
buffer.close();
BufferedReader readFile = new BufferedReader(new FileReader(file1));
String get_text = "";
String lines = null;
while ((lines = readFile.readLine()) != null) {
get_text += lines; // Gets each line
}
readFile.close();
//THE PROBLEM SEEMS TO START HERE
byte[] dec = get_text.getBytes(Charset.forName("UTF-8"));
Log.e("TEXT", get_text);
Log.e("BYTES FROM TEXT", Arrays.toString(dec));
} else {
System.out.println("File Already Exist");
}
} catch (Exception e) {
e.getCause();
}
}catch (Exception e){
e.printStackTrace();
}
日志:
04-24 16:41:42.208 2934-2934/com.test.test E/ENCRYPTED TEXT﹕ ϩ{���
04-24 16:41:42.218 2934-2934/com.test.test E/ENCRYPTED TEXT﹕ [-49, -87, 123, -15, 1, -84, -11]
04-24 16:41:42.218 2934-2934/com.test.test E/BYTES FROM TEXT1﹕ [-49, -87, 123, -15, 1, -84, -11]
04-24 16:41:42.218 2934-2934/com.test.test E/STRINGGGGG﹕ ϩ{���
04-24 16:41:42.218 2934-2934/com.test.test E/TEXT﹕ ϩ{���
04-24 16:41:42.218 2934-2934/com.test.test E/BYTES FROM TEXT﹕ [-49, -87, 123, -17, -65, -67, 1, -17, -65, -67, -17, -65, -67]
为什么返回不同的字节值?感谢您的帮助,谢谢。
已编辑。
在
处将 file
替换为 file1
new BufferedWriter(new FileWriter(file1));
和
new BufferedReader(new FileReader(file1));
try {
String y = "Yyyyyyy";
byte[] a = y.getBytes(Charset.forName("UTF-8"));
Log.e("TEXT", y);
Log.e("BYTES FROM TEXT", Arrays.toString(a));
try {
File file1 = new File("test.txt");
if (file1.createNewFile()) {
String example = new String(a);
BufferedWriter buffer = new BufferedWriter(new FileWriter(file1));
buffer.write(example);
buffer.flush();
buffer.close();
BufferedReader readFile= new BufferedReader(new FileReader(file1));
String get_text = "";
String lines = null;
while ((lines = readFile.readLine()) != null){
get_text += lines; // Gets each line
}
byte[] dec = get_text.getBytes(Charset.forName("UTF-8");
Log.e("TEXT", get_text);
Log.e("BYTES FROM TEXT", Arrays.toString(dec));
} else {
System.out.println("File Already Exist");
}
} catch(Exception e) {
e.getCause();
}
} catch(Exception e){
e.getMessage();
}
我的输出:
TEXT Yyyyyyy
BYTES FROM TEXT [89, 121, 121, 121, 121, 121, 121]
TEXT Yyyyyyy
BYTES FROM TEXT [89, 121, 121, 121, 121, 121, 121]
您的代码的一个主要错误是您将字符串误认为字节数组。
如果您正在编写字符串,请直接使用 Writer 编写它们。
然后您可以删除无用的转换,如下所示:
byte[] dec = get_text.getBytes(Charset.forName("UTF-8");
它将 String 转换为 byte[],然后再转换回 String。
结论:
如果你想指定一个字符集来保存一个字符串,使用 String.getBytes("UTF-8")
然后将结果以 byte[] 形式写入一个 OutputStream。
或者,您可以在使用构造函数时直接编写字符集感知的字符串 OutputStreamWriter(OutputStream out, String charsetName)
类似的规则适用于读取文件。
我正在尝试将字符串字节存储到一个文本文件中,它在我的电脑上运行良好,但是当我尝试将它实现到我的 android 项目中时,它存储了它,但是当我想取回字符串字节时并将其转换为它不起作用的原始字节。它再次在我的电脑上运行,我不知道为什么它在我的 android 项目上不起作用。
代码:
try {
String y = "Yyyyyyy";
try {
File file1 = new File(Environment.getExternalStorageDirectory() + File.separator + "test/test/newFile.txt");
if (!file1.createNewFile()) {
EncryptedObject a = encryptedMessage.encrypt(y, "test", "test");
Log.e("BYTES FROM TEXT1", Arrays.toString(a.getEncryptedBytes()));
String example = new String(a.getEncryptedBytes());
Log.e("STRINGGGGG", example);
BufferedWriter buffer = new BufferedWriter(new FileWriter(file1));
buffer.write(example);
buffer.flush();
buffer.close();
BufferedReader readFile = new BufferedReader(new FileReader(file1));
String get_text = "";
String lines = null;
while ((lines = readFile.readLine()) != null) {
get_text += lines; // Gets each line
}
readFile.close();
//THE PROBLEM SEEMS TO START HERE
byte[] dec = get_text.getBytes(Charset.forName("UTF-8"));
Log.e("TEXT", get_text);
Log.e("BYTES FROM TEXT", Arrays.toString(dec));
} else {
System.out.println("File Already Exist");
}
} catch (Exception e) {
e.getCause();
}
}catch (Exception e){
e.printStackTrace();
}
日志:
04-24 16:41:42.208 2934-2934/com.test.test E/ENCRYPTED TEXT﹕ ϩ{���
04-24 16:41:42.218 2934-2934/com.test.test E/ENCRYPTED TEXT﹕ [-49, -87, 123, -15, 1, -84, -11]
04-24 16:41:42.218 2934-2934/com.test.test E/BYTES FROM TEXT1﹕ [-49, -87, 123, -15, 1, -84, -11]
04-24 16:41:42.218 2934-2934/com.test.test E/STRINGGGGG﹕ ϩ{���
04-24 16:41:42.218 2934-2934/com.test.test E/TEXT﹕ ϩ{���
04-24 16:41:42.218 2934-2934/com.test.test E/BYTES FROM TEXT﹕ [-49, -87, 123, -17, -65, -67, 1, -17, -65, -67, -17, -65, -67]
为什么返回不同的字节值?感谢您的帮助,谢谢。 已编辑。
在
处将file
替换为 file1
new BufferedWriter(new FileWriter(file1));
和
new BufferedReader(new FileReader(file1));
try {
String y = "Yyyyyyy";
byte[] a = y.getBytes(Charset.forName("UTF-8"));
Log.e("TEXT", y);
Log.e("BYTES FROM TEXT", Arrays.toString(a));
try {
File file1 = new File("test.txt");
if (file1.createNewFile()) {
String example = new String(a);
BufferedWriter buffer = new BufferedWriter(new FileWriter(file1));
buffer.write(example);
buffer.flush();
buffer.close();
BufferedReader readFile= new BufferedReader(new FileReader(file1));
String get_text = "";
String lines = null;
while ((lines = readFile.readLine()) != null){
get_text += lines; // Gets each line
}
byte[] dec = get_text.getBytes(Charset.forName("UTF-8");
Log.e("TEXT", get_text);
Log.e("BYTES FROM TEXT", Arrays.toString(dec));
} else {
System.out.println("File Already Exist");
}
} catch(Exception e) {
e.getCause();
}
} catch(Exception e){
e.getMessage();
}
我的输出:
TEXT Yyyyyyy
BYTES FROM TEXT [89, 121, 121, 121, 121, 121, 121]
TEXT Yyyyyyy
BYTES FROM TEXT [89, 121, 121, 121, 121, 121, 121]
您的代码的一个主要错误是您将字符串误认为字节数组。 如果您正在编写字符串,请直接使用 Writer 编写它们。
然后您可以删除无用的转换,如下所示:
byte[] dec = get_text.getBytes(Charset.forName("UTF-8");
它将 String 转换为 byte[],然后再转换回 String。
结论:
如果你想指定一个字符集来保存一个字符串,使用 String.getBytes("UTF-8")
然后将结果以 byte[] 形式写入一个 OutputStream。
或者,您可以在使用构造函数时直接编写字符集感知的字符串 OutputStreamWriter(OutputStream out, String charsetName)
类似的规则适用于读取文件。