我正在尝试检查两个字符串是否相同,第一个字符串是我从Pastebin RAW获取的,第二个字符串是我保存在项目中的asset文件夹中的。文字完全相同,但是当我尝试检查它们是否相同时
if(total.toString().equals(result)){
display.setText(
"The two files are the same \n Log.txt: " + total.toString() +
"\n Pastebin: " + result);
} else if(total.toString()!=result) {
display.setText(
"The two files arent the same \n Log.txt: " + total.toString() +
"\n Pastebin: " + result);
它直接显示我的其他情况,并显示该错误,我已尝试删除该文件并制作新的Pastebins。
我使用的完整代码是这样
InputStream is = getAssets().open("Log.txt");
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
// Loads the text from the pastebin into the string result
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("Pastebin url");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()));
String line1 = null;
while ((line1 = reader.readLine()) != null){
result += line1 + "\n";
}
// Checks if the pastebin and Log.txt contains the same information
if( total.toString().equals(result)){
display.setText(
"The two files are the same \n Log.txt: " + total.toString() +
"\n Pastebin: " + result);
} else if(total.toString()!=result) {
display.setText(
"The two files arent the same \n Log.txt: " + total.toString() +
"\n Pastebin: " + result);
}
那么有人可以告诉我我在这里做错了什么吗,因为它说那是不一样的?
最佳答案
问题出在以下几行:
while ((line = r.readLine()) != null) {
total.append(line);
}
您会忘记换行符
\n
:while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
与您在
result
中所做的一样:while ((line1 = reader.readLine()) != null){
result += line1 + "\n";
}
另外请注意
else if(total.toString()!=result)
应该只是
else
关于java - 在Android中检查两个字符串是否相同时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15145054/