从android中的sdcard读取特定文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3779944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading a specific file from sdcard in android
提问by sajjoo
how to read a specific file from sdcard. i have pushed the file in sdcard through DDMS and i am trying to read it though this way but this give me exception. can anybody tell me how to point exactly on that file?
如何从sdcard读取特定文件。我已经通过 DDMS 将文件推送到 sdcard 中,我试图通过这种方式读取它,但这给了我一个例外。谁能告诉我如何准确指向该文件?
my code is this.
我的代码是这样的。
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream = new FileInputStream(path);
回答by Cristian
You are trying to read a directory... what you need is the file! Do something like this... then, you can read the file as you want.
您正在尝试读取目录...您需要的是文件!做这样的事情......然后,您可以根据需要读取文件。
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
回答by JAYESH
To read any file(CSV in my case) from External Storage, we need a path for it,once you have path you can do like this...
要从外部存储读取任何文件(在我的情况下为 CSV),我们需要一个路径,一旦你有了路径,你就可以这样做......
void readFileData(String path) throws FileNotFoundException
{
String[] data;
File file = new File(path);
if (file.exists())
{
BufferedReader br = new BufferedReader(new FileReader(file));
try
{
String csvLine;
while ((csvLine = br.readLine()) != null)
{
data=csvLine.split(",");
try
{
Toast.makeText(getApplicationContext(),data[0]+" "+data[1],Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("Problem",e.toString());
}
}
}
catch (IOException ex)
{
throw new RuntimeException("Error in reading CSV file: "+ex);
}
}
else
{
Toast.makeText(getApplicationContext(),"file not exists",Toast.LENGTH_SHORT).show();
}
}
/*
csv file data
17IT1,GOOGLE
17IT2,AMAZON
17IT3,FACEBOOK*/