为什么我无法读取SD中的文本文件?

时间:2013-11-12 09:20:23

标签: java android text bufferedreader

我可以在我的日志中读到我在sdcard的txt文件中写的内容,但我无法打开该文件。我需要onClick打开一个txt查看器或其他文件..我可以用这种方式在日志中显示值:

public void onClick(View v) 
            {

                File file = new File("/sdcard/ReportData.txt");
                StringBuffer contents = new StringBuffer();
                BufferedReader reader = null;

                try {
                    reader = new BufferedReader(new FileReader(file));
                    String text = null;

                    // repeat until all lines is read
                    while ((text = reader.readLine()) != null) {
                        contents.append(text)
                            .append(System.getProperty(
                                "line.separator"));
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }           

                Log.e("TEXT", contents.toString());
            }

但是我无法打开文件..我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

试试这个..

public void onClick(View v) 
{
          Intent intent = new Intent();
          intent.setAction(android.content.Intent.ACTION_VIEW);
          File file = new File("/sdcard/ReportData.txt");
          intent.setDataAndType(Uri.fromFile(file), "text/*");
          startActivity(intent); 
}

答案 1 :(得分:1)

请尝试以下代码。以下代码在textedit中显示文本文件。

 //Find the view by its id
    TextView tv = (TextView)findViewById(R.id.fileContent);

public void onClick(View v) 
        {

            File file = new File("/sdcard/ReportData.txt");
            StringBuffer contents = new StringBuffer();
            BufferedReader reader = null;

            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;

                // repeat until all lines is read
                while ((text = reader.readLine()) != null) {
                    contents.append(text)
                        .append(System.getProperty(
                            "line.separator"));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }           

            Log.e("TEXT", contents.toString());
             String text = contents.toString();

  //Set the text
        tv.setText(text);
        }

希望这对你有所帮助!! 有关详细信息,您可以浏览android-read-text-file-from-sd-card