【问题标题】:code to read text from sd not working从 sd 读取文本的代码不起作用
【发布时间】:2014-02-05 20:51:20
【问题描述】:

我正在尝试从我的 sd 中读取文件 numbersmesa11,但我在最后的 while 子句中有一个错误,我不知道该怎么做,我将不胜感激!我真的卡住了

private void readVectorFromFile() {
 InputStreamReader flujo=null;
    BufferedReader lector=null;
    try
    {
        File ruta = Environment.getExternalStorageDirectory();
        File fichero = new File(ruta.getAbsolutePath()+"/BioCasinoStats", "numerosmesa11.txt");                
        flujo= new InputStreamReader(new FileInputStream(fichero));
        lector= new BufferedReader(flujo);
        String texto = lector.readLine();
        while(texto!=null)
        {
            tbTexto.setText(texto);
            texto=lector.readLine();
        }
    }
    catch (Exception ex)
    {
        Log.e("ivan", "Error al leer fichero desde tarjeta SD");
    }
    finally
    {
        try {
            if(lector!=null)
                lector.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 确保所有 catch 块都打印堆栈跟踪,从 logcat 获取,然后发布。
  • 请发布您遇到的错误
  • I have an error in the final while clause - 什么错误?
  • 说tbtexto无法解析
  • it says tbtexto cannot be resolved。和读文件有什么关系?

标签: java android android-sdcard


【解决方案1】:

首先确保您的清单权限设置正确。要访问外部存储,您应该使用 android.permission.READ_EXTERNAL_STORAGE

查看文档:http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

如果您已经这样做了,请尝试发布错误消息以帮助我们识别您的问题。

【讨论】:

    【解决方案2】:

    如果是说变量tbTexto无法解析,检查这个变量的作用域,或者这个变量是否存在于这个作用域。

    还要更改 catch 块以打印堆栈跟踪,没有有用信息的通用 catch 是没有意义的,因为您不会知道哪个问题导致了预期。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2012-09-20
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多