从Sdcard动态读取文件 [英] Dynamically read files from Sdcard

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

问题描述



下面是我的代码阅读文件:

 文件f = new File(Environment.getExternalStorageDirectory()+/ f1.txt); 
fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//只是读取每一行并将其传递给调试器
while((readString = buf.readLine())!= null){
textdata.setText(readString);
Log.d(line:,readString);
}
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

但是我需要动态地从sdcard读取文件。



这里我已经把它作为 f1.txt

解决方案

尝试这样:

 文件f = new File(Environment.getExternalStorageDirectory()。toString()+ /音频); 
if(f.isDirectory())
{
String files [] = f.list(); (int i = 0; i< files.length; i ++)
{
Log.d(,files [i]);

}
}


Guys i have a text file in sdcard i need to read that file.

Below is my code to read files:

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

But i need to read files from sdcard dynamically.

here i have given it as f1.txt

解决方案

Try this:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆