如何从SD卡读取特定文件。我已经通过DDMS在sdcard中推送文件,我试图通过这种方式阅读它,但这给了我一个例外。任何人都可以告诉我如何准确指出该文件?
我的代码就是这个。
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream = new FileInputStream(path);
答案 0 :(得分:47)
您正在尝试阅读目录...您需要的是文件!做这样的事情......然后,你可以根据需要阅读文件。
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
答案 1 :(得分:0)
要从外部存储中读取任何文件(在我的情况下为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*/