本文介绍了Android从SD卡上的文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我在SD卡上创建的文本文件中读取一些数据.但是,当我读完它时,stringbuilder仍然是空白的.我不知道它是否不读取文件.我在android设备监视器上看了看,fie存在其中的数据.它在我的sdcard上的"Notes"目录中.这是代码.

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

推荐答案

尝试进行如下更改

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

    }
});

这篇关于Android从SD卡上的文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:09