【问题标题】:Storing and Retrieving the data to the Internal Storage Error ,Android Studio?将数据存储和检索到内部存储错误,Android Studio?
【发布时间】:2021-04-02 17:30:06
【问题描述】:

所以我试图将数据存储到我的内部存储并取回它,所以当我尝试取回数据时它会检索数据但是当我在条件语句中使用它时它不起作用

所以这些是存储和检索数据的方法

储存方法

 // storing the credentials into the internal storage method

public void savedata(String FIlename, String datatobeStored) {
    FileOutputStream fos = null;
    try {
        // storing at the parameter fileLocation
        fos = openFileOutput(FIlename, MODE_PRIVATE);
        // storint the parameter  text Given
        fos.write(datatobeStored.getBytes());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

检索方法

// Retreiving  the credentials  from the the internal storage meathod
public String dataRetreive(String filename) {
    String variableTogetStored = "qqq";
    FileInputStream fis = null;
    try {
        fis = openFileInput(filename);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String text;
        while ((text = br.readLine()) != null) {
            sb.append(text).append("\n");
        }
        
        // retreiving the value from the storage and assigning it to the local variable
        variableTogetStored = sb.toString();
        
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // returing the local variable
    return variableTogetStored;
}

当我尝试检索和调节语句时它不起作用

存储数据

 savedata("teacherAdmin", "dashboardToTeachersAdmin");

检索数据

 String fromwheressss = dataRetreive("teacherAdmin");

条件语句

                if (fromwheressss.equals("dashboardToTeachersAdmin")) {

                    Toast.makeText(teacher_admin.this,
                            "Matched", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(teacher_admin.this,
                            "Not Matched", Toast.LENGTH_SHORT).show();

                }

这个条件给了我不匹配*

即使我尝试将检索到的字段打印到 Toast,它也会打印出有点奇怪,下面有完整的空间

【问题讨论】:

    标签: java android internal-storage


    【解决方案1】:

    在检索数据时,您在每行之后附加 "\n" 并且 "dashboardToTeachersAdmin\n".equals("dashboardToTeachersAdmin") 始终为 false。所以最后这样做,你会得到正确的条件结果。

    fromwheressss.trim().equals("dashboardToTeachersAdmin")
    

    或者只是不附加 "\n"。如果您存储和检索不同的数据,仍然有可能使用 equals() 得到错误的条件结果。

    【讨论】:

    • 不,这也不起作用。所以我找到了一个替代解决方案,我回答说,你可以检查一下。
    【解决方案2】:

    所以为了正常工作,我将其更改为共享首选项,除此之外没有任何工作

    这是代码

    存储功能

        // storing the credentials in to the internal storage meathod
    
    public void savedata(String prefernecename, String dataKey, String datatobeStored) {
    
        // preference name and data key are used to retrice the data and data to be stored is the
        // value you wanna store
        SharedPreferences sharedPreferences = getSharedPreferences(prefernecename, Context.MODE_PRIVATE);
    
        SharedPreferences.Editor editor = sharedPreferences.edit();
    
        editor.putString(dataKey, datatobeStored);
        editor.commit();
    
    }
    

    检索值

    // Retreiving  the credentials  from the the internal storage meathod
    public String dataRetreive(String prefernecename, String dataKey, String defaultValue) {
    
        // default value is somethhing which will be returned when no data is found place something
        //like "ERROR" in defaultValue
        SharedPreferences sharedPreferences = getSharedPreferences(prefernecename, Context.MODE_PRIVATE);
    
        String storeddata = sharedPreferences.getString(dataKey, defaultValue);
    
        // returing the local variable
        return storeddata;
    }
    

    现在完美运行

       // retreiving the value
                    String fromwheressss = dataRetreive("teacherAdmin",
                            "teacherAdminKey", defaultvalueForSharedpref);
    
    
    
                    // removing the view based on the acitvity
    
                    if (fromwheressss.equals("dashboardToTeachersAdmin")) {
    
                        Toast.makeText(teacher_admin.this,
                                "Matched", Toast.LENGTH_SHORT).show();
    
                    } else {
                        Toast.makeText(teacher_admin.this,
                                "Not Matched", Toast.LENGTH_SHORT).show();
    
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2015-06-14
      • 1970-01-01
      相关资源
      最近更新 更多