如何从SD卡中读取特定文件。我已经通过DDMS将文件推送到SD卡中,并尝试通过以下方式读取它,但是这会给我一个异常。有人能告诉我如何准确地指向那个文件吗?
我的代码如下。
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream = new FileInputStream(path);
如何从SD卡中读取特定文件。我已经通过DDMS将文件推送到SD卡中,并尝试通过以下方式读取它,但是这会给我一个异常。有人能告诉我如何准确地指向那个文件吗?
我的代码如下。
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream = new FileInputStream(path);
您试图读取一个目录...而您需要的是文件!请像这样做...然后,您可以按照所需读取文件。
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
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*/