Read/Write and compare android external storage

Viewed 59
public class PaymentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        String FILENAME = "paid";
       String data = "yes";

        File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);


        try {
            File myFile = new File(folder, FILENAME);
            FileOutputStream fstream = new FileOutputStream(myFile);
            fstream.write(data.getBytes());
            fstream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            File myFile = new File(folder, FILENAME);
            FileInputStream fstream = new FileInputStream(myFile);
            StringBuilder sbuffer = new StringBuilder();
            int i;
            while ((i = fstream.read())!= -1){
                sbuffer.append((char)i);
            }

            String haspaid = sbuffer.toString();
            System.out.println("Help!"+haspaid.equals("yes"));
            if (haspaid.equals("yes")) {

                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            }
            fstream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

I have a file write that inputs "yes" and a file read the reads that "yes" on external storage. I have System.out.println printed it out, and the file read/write seems to work. And yet somehow, when I compare the string resulted, it cannot be checked if it is a value.

What am I doing wrong?

1 Answers

use equals to check the equality of string instead of "!=" or "==".
equals checks the value, and "!=" or "==" checks the reference.

Related