reading a specific file from sdcard in android-ThrowExceptions

Exception or error:

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?

my code is this.

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream =  new FileInputStream(path);
How to solve:

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");

###

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…

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*/

Leave a Reply

Your email address will not be published. Required fields are marked *