简体   繁体   English

android外存读写比较

[英]Read/Write and compare android external storage

 String FILENAME = "paid";
        String data = "no";

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

            if (haspaid != "no") {
                     // this should not work
                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 "no" and a file read the reads that "no" on external storage.我有一个文件写入输入“否”,一个文件读取外部存储上的“否”。 I have System.out.println printed it out, and the file read/write seems to work.我已将 System.out.println 打印出来,文件读/写似乎工作。 And yet somehow, when I compare the string resulted, it cannot be checked if it is a value.然而不知何故,当我比较结果字符串时,无法检查它是否是一个值。

What am I doing wrong?我究竟做错了什么?

use equals to check the equality of string instead of ".=" or "==".使用equals代替 ".=" 或 "==" 检查字符串的相等性。
equals checks the value, and ".=" or "==" checks the reference. equals检查值,".=" 或 "==" 检查引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM