Android reading from file on SD Card

2019-08-12 10:57发布

I am trying to read in some data from a text file I created on my SDcard. However, when I am done reading it in, the stringbuilder is still blank. I don't know if it isn't reading the file. I looked on the android device monitor and the fie exists with data in it. It is on my sdcard inside a "Notes" directory. Here is the code.

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    welcomeText = (TextView) findViewById(R.id.welcomeText);
    userName = (EditText) findViewById(R.id.userName);
    submitButton = (Button) findViewById(R.id.submitButton);
    password = (EditText) findViewById(R.id.passInput);
    viewAll = (Button) findViewById(R.id.viewAll);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            generateNoteOnSD("info", userName.getText().toString() + "," +password.getText().toString() );
        }
    });

    viewAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File sdCard = Environment.getExternalStorageDirectory();
            File file = new File(sdCard, "info");
            String line;

            StringBuilder text = new StringBuilder();

            try {
                BufferedReader br = new BufferedReader(new FileReader(file));


                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }

                br.close();
            }
            catch (IOException e) {

            }

            welcomeText.setText("Welcome, " + text);

        }
    });
}

public void generateNoteOnSD(String sFileName, String sBody){
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

1条回答
Juvenile、少年°
2楼-- · 2019-08-12 11:44

Try to change like below

viewAll.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String fileName = "/Notes/info";
            File myFile = new File(baseDir + File.separator + fileName);

    try {

            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(
                    new InputStreamReader(fIn));
            String text = "";
            String aDataRow = "";
            while ((aDataRow = myReader.readLine()) != null) {
                text += aDataRow + "\n";
            }

        }
        catch (IOException e) {

        }

        welcomeText.setText("Welcome, " + text);

    }
});
查看更多
登录 后发表回答