Search code examples
javaandroidinternal-storage

Storing and Retrieving the data to the Internal Storage Error ,Android Studio?


So I am trying to store the data to my internal storage and Retrieve it back, So when I try to retrieve the data back it retrieves the data but when I use it in the Conditional Statements it doesn't work

so these are the methods to store and retrieve the data

STORING MEATHOD

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

RETRIEVAL MEATHOD

// 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;
}

When I try to retrieve and condition the statement it doesn't work

Storing the Data

 savedata("teacherAdmin", "dashboardToTeachersAdmin");

Retrieving data

 String fromwheressss = dataRetreive("teacherAdmin");

Condition statement

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

                }

This condition gives me Not matched*

Even I tried printing the retrieved field to the Toast it prints kinda Weird with complete space below


Solution

  • While retreiving data you are appending "\n" after each line and "dashboardToTeachersAdmin\n".equals("dashboardToTeachersAdmin") is always false. So finally do this and you will get the right conditional result.

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

    or just don't append "\n". There are still possibilities you will get the wrong conditional result using equals() if you store and retreive a different data.