我正在尝试检查两个字符串是否相同,我从Pastebin RAW获得的第一个字符串,以及我在项目中的assets资源文件夹中保存的第二个字符串。文本完全相同,但是当我尝试检查它们是否与
相同时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);
}
所以有人可以告诉我我在这里做错了什么,因为它说它不一样了吗?
答案 0 :(得分:3)
问题在于以下几点:
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