简体   繁体   中英

Will Android override file?

I've got following question: will Android's service override file with method posted below? What I want to get is to download file everytime the app runs - via service, but I'm not sure if the file is overridden. I've tried to pull the file from Android File Explorer and check the date & time of creation, but it always had the time of pulling from device, not the time the file was actually created. Posting my method below:

public static void downloadFile(String sUrl, String fileName, String sharedPref, Context context) {
    try {
        HttpClient client = new CustomHttpClient(
                context.getApplicationContext());
        HttpGet request = new HttpGet(sUrl);
        HttpResponse response = client.execute(request);
        String responseAsString = EntityUtils.toString(response.getEntity());
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                context.openFileOutput(fileName, Context.MODE_PRIVATE));
        outputStreamWriter.write(responseAsString);
        outputStreamWriter.close();
        PreferenceManager
                .getDefaultSharedPreferences(context)
                .edit().putString(sharedPref, "1").commit();
    } catch (Exception e) {
        System.out.println("Exc=" + e);
    }
}

By default android will not overWrite you file. You need to make it programmatically. This link may help you - How do you overwrite instead of append to a text file in Android?

flag the filewrite with false as second argument, this way it will over-write your file. cheers

I've tested it by replacing url of existing file and it overrided it. Anyways, thanks for answers - will remember that when using FileWriter class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM