0

我正在尝试检查两个字符串是否相同,第一个字符串是我从 Pastebin RAW 获得的,第二个字符串是我保存在项目的资产文件夹中的。文本完全相同,但是当我尝试检查它们是否相同时

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);    

它直接转到我的 else if 并显示,我尝试删除文件并制作新的 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); 
   }

那么任何人都可以告诉我我在这里做错了什么,因为它说它不一样吗?

4

1 回答 1

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
于 2013-02-28T20:34:43.300 回答