Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将多个字符串存储和检索到单个文件_Android_Eclipse_Android Activity - Fatal编程技术网

Android 将多个字符串存储和检索到单个文件

Android 将多个字符串存储和检索到单个文件,android,eclipse,android-activity,Android,Eclipse,Android Activity,我试图将用户名和密码(两个字符串)存储到同一个文件中。存储部分对我来说很好,但在检索部分似乎有一些错误。我似乎找不到解决办法。有人能帮我吗。。。我存储和检索字符串到文件的代码如下所示: public void startApp() { try{ File setFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/code.r

我试图将用户名和密码(两个字符串)存储到同一个文件中。存储部分对我来说很好,但在检索部分似乎有一些错误。我似乎找不到解决办法。有人能帮我吗。。。我存储和检索字符串到文件的代码如下所示:

 public void startApp()
   {

     try{   
        File setFile=new        
        File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/code.rhn");
        System.out.println(setFile.getAbsolutePath());
        if(!setFile.exists()){

           //writing data to file
              FileOutputStream fOut = new FileOutputStream(setFile);
              OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
              myOutWriter.append(PuserName.toString());
              myOutWriter.append("\n");
              myOutWriter.append(Ppass.toString());
              myOutWriter.close();
              fOut.close();
              startPocket();

          }
     else
          {


         //reading two strings from file
              FileInputStream fin=new FileInputStream(setFile);
              InputStreamReader isr = new InputStreamReader(fin);
              BufferedReader bufferedReader = new BufferedReader(isr);
              String receiveString1 = "";
              String receiveString2 = "";
              StringBuilder stringBuilder = new StringBuilder();
        //storing string1 to temp1      
              receiveString1 = bufferedReader.readLine(); 
              stringBuilder.append(receiveString1);
              temp1 = stringBuilder.toString();
        //storing string2 to temp2                     
              receiveString2 = bufferedReader.readLine(); 
              stringBuilder.append(receiveString2);
              temp2=stringBuilder.toString();
              fin.close();


        // checking for identical username and password                     
              if ((temp1== (PuserName.toString()+"\n"))&&(temp2==Ppass.toString()))
                         {
                           startPocket();
                         }
                     else
                         startWrongWay();

         }
       }catch(Exception e){
            Toast.makeText(this, "exception occured", Toast.LENGTH_LONG).show();
         }  

    }

每次我在写入inetial用户名和密码后执行此代码时,仅会执行startBlongway(),而startPocket()永远不会执行,即使我键入了正确的用户名和密码。

您在比较字符串时犯了基本错误。您正在使用“==”,其中应使用“equals()”。几分钟后,我将检查代码中是否存在其他问题

检查后,您还有一个问题。根本不需要StringBuilder。由于使用不正确,temp2最终是登录名和密码的总和。修复此问题+使用前面提到的等于(但内部没有“\n”)应该可以使代码正常工作。

使用
temp1.equals(PuserName.toString())
而不是
=
。如果使用
=
比较引用,则使用
equals方法比较值


另外,您不必与
PuserName.toString()+“\n”
进行比较,因为方法
readLine
不包含任何行终止字符。

我尝试使用.equals,但错误仍然存在。我似乎找不到它是什么。试着不用StringBuilder。只需使用:
temp1=bufferedReader.readLine
(与temp2相同)。在您的if使用中:
temp1.equals(PuserName.toString()&&temp2.equals(Ppass.toString())
。如果仍然不工作,可能是您的toString方法没有很好地实现。感谢很多朋友。删除字符串生成器对我来说很有效。我的代码现在运行良好。