从Sdcard动态读取文件

时间:2012-03-01 12:33:27

标签: android dynamic android-file

伙计们我在sdcard中有一个文本文件,我需要阅读该文件。

以下是我读取文件的代码:

 File f = new File(Environment.getExternalStorageDirectory()+"/f1.txt");
       fileIS = new FileInputStream(f);
            BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
           String readString = new String(); 
           //just reading each line and pass it on the debugger
           while((readString = buf.readLine())!= null){
              textdata.setText(readString);
              Log.d("line: ", readString);
           }
        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch (IOException e){
           e.printStackTrace();
        }

但我需要动态地从SD卡读取文件。

这里我已将其作为f1.txt

给出

1 个答案:

答案 0 :(得分:3)

试试这个:

File f = new File(Environment.getExternalStorageDirectory().toString() + "/audio");
if (f.isDirectory())
{
    String files[] = f.list();
    for (int i = 0; i < files.length; i++)
    {
        Log.d("", files[i]);
    }
}