I am writing trial start date to a file. I don't want the user to be
able to delete this file. How can I make sure that "Clear Data" won't
clear this file. Currently, the behavior I have seen is that the file
is deleted if user clicks "clear data". Here is my code in case it
helps.

private static final String TRIAL_FILE_NME="trial.file" ;
private static final String TRIAL_DIR_NME="trial";

private boolean setTrialStartDate (){

        FileOutputStream fileWriter = null ;
        boolean returnValue = true ;

        try{
                File dir = getDir(TRIAL_DIR_NME, MODE_PRIVATE);
                File trialFile = new File(dir, TRIAL_FILE_NME);

                fileWriter = openFileOutput(trialFile.getName(),
MODE_PRIVATE);
 
fileWriter.write(DateTimeUtil.getCurrentDateString().getBytes());
                fileWriter.flush();
        }
        catch (FileNotFoundException filenotFound){
                returnValue = false ;
        }
        catch (IOException exception){
                returnValue = false ;
        }
        finally{
                if ( fileWriter != null ){
                        try{
                                fileWriter.close();
                        }
                        catch (IOException exc){
                                fileWriter = null ;
                                returnValue = false ;
                        }
                }
        }

        return returnValue ;
    }

    private String getTrialStartDate (){

        FileInputStream fileReader = null ;
        String dateString = null ;

        try{
                File dir = getDir(TRIAL_DIR_NME, MODE_PRIVATE);
                File trialFile = new File(dir, TRIAL_FILE_NME);
                fileReader = openFileInput(trialFile.getName());
                byte[] totalBytes = new byte[fileReader.available()];

                if ( fileReader.read(totalBytes) != -1 ){
                        dateString = new String (totalBytes);
                }
        }

        catch (FileNotFoundException exception){
                setTrialStartDate();
                dateString = DateTimeUtil.getCurrentDateString();
        }
        catch (IOException exc){
                //Error reading file. Simply return null
                dateString = null ;
        }
        finally{
                if ( fileReader != null ){
                        try{
                                fileReader.close();
                        }
                        catch (IOException exc){
                                //Although it might be safe to return what's in 
dateString at
this
                                //point, but since there is an I/O issue, it is 
better to
return
                                //null
                                dateString = null ;
                                fileReader = null ;
                        }
                }
        }

        return dateString ;
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to