简体   繁体   English

Android会覆盖文件吗?

[英]Will Android override file?

I've got following question: will Android's service override file with method posted below? 我有以下问题:Android的服务覆盖文件的方法是否发布在下面? 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. 我试图从Android文件资源管理器中提取文件并检查创建的日期和时间,但它始终有从设备中提取的时间,而不是文件实际创建的时间。 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. 默认情况下,android不会覆盖你的文件。 You need to make it programmatically. 您需要以编程方式进行。 This link may help you - How do you overwrite instead of append to a text file in Android? 此链接可以帮助您 - 如何覆盖而不是附加到Android中的文本文件?

flag the filewrite with false as second argument, this way it will over-write your file. 将filewrite标记为false作为第二个参数,这样它将覆盖您的文件。 cheers 干杯

I've tested it by replacing url of existing file and it overrided it. 我已经通过替换现有文件的url来测试它并且它覆盖了它。 Anyways, thanks for answers - will remember that when using FileWriter class. 无论如何,感谢您的答案 - 将记住使用FileWriter类时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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