将数据存储和检索到内部存储错误,Android Studio?

问题描述

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

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

储存方法

 // 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 它打印有点奇怪,下面有完整的空间

解决方法

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

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

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

,

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

这是代码

存储功能

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

                }