将数据存储和检索到内部存储错误,Android Studio?
Storing and Retrieving the data to the Internal Storage Error ,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 上,它打印的内容也有点奇怪,下面是完整的 space
检索数据时,您在每行后附加 "\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 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();
}
所以我试图将数据存储到我的内部存储中并将其取回,因此当我尝试取回数据时它会检索数据但是当我在条件语句中使用它时它不工作
这些是存储和检索数据的方法
存储方法
// 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 上,它打印的内容也有点奇怪,下面是完整的 space
检索数据时,您在每行后附加 "\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 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();
}