Nick
Nick

Reputation: 2641

Android : App crashes when there is no file to load, how do i over come this?

My app crashes when it tryes to load a file that isnt there. how do i stop it from loading the file when there isnt a file but when there is a file of that name it does load it.

Here is the code I am using to load the file. Any comments would be appreciated :)

Cheers

    try {
        fis = openFileInput(FILENAME1);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1){
            task1 = new String(dataArray);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Upvotes: 0

Views: 148

Answers (1)

theWalker
theWalker

Reputation: 2020

Try something like this:

fis = getContext().getFileStreamPath(FILENAME1);
if( fis.exists() ) {
...
}
else {
....
}

Upvotes: 2

Related Questions