Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我能';你不能在sd中读取文本文件吗?_Java_Android_Text_Bufferedreader - Fatal编程技术网

Java 为什么我能';你不能在sd中读取文本文件吗?

Java 为什么我能';你不能在sd中读取文本文件吗?,java,android,text,bufferedreader,Java,Android,Text,Bufferedreader,我可以在我的日志中读取我在SD卡的txt文件中写入的内容,但我无法打开该文件。我需要用txt查看器或其他工具单击打开文件。。我现在可以通过以下方式在日志中显示值: public void onClick(View v) { File file = new File("/sdcard/ReportData.txt"); StringBuffer contents = new StringBuffer();

我可以在我的日志中读取我在SD卡的txt文件中写入的内容,但我无法打开该文件。我需要用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());
            }
但是我无法打开文件。。我怎么做呢?

试试这个

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

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

 //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);
        }
希望这对你有帮助!!
有关详细信息,请浏览

是否确定该文件存在?您确定路径正确吗?还提供发生错误的日志…希望如此,在清单文件中定义了必要的权限。我已经写入了权限,但什么也没有发生。您是否检查了这些链接1。2.同样的事情。。我可以在日志中显示:
TEXT 3,88 MB 34,78 MB
,但当我click@David_D我知道我会尽快发帖的!!这就是我想要的!多谢各位@Tamilan@David_D很高兴它能帮上忙。它能在文本中显示txt文件中的数据吗?我需要一些东西来打开文件。如果可能的话,在android txt默认查看器中,我忘了将文本初始化为“字符串”。我以为你想在应用程序中打开。不管怎样,你找到了解决办法。干杯不管怎样,我接受你的回答只是因为你帮助了我;)。拜伊