在内部存储重命名文件 [英] Rename a file in the internal storage

查看:181
本文介绍了在内部存储重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是重命名应用程序的内部存储的文件的最好/最简单的方法是什么?我觉得有点怪,有一个 Context.deleteFile()方法,但没有移动或重命名功能。我必须通过保存文件的内容,删除它,创建一个新的,然后将内容复制到一个去所有的方式?或者是有办法的文件在现有文件复制?

What's the best/easiest way to rename a file in the application's internal storage? I find it a bit strange that there is a Context.deleteFile() method, but no "move" or "rename" function. Do I have to go all the way through saving the file's contents, deleting it, creating a new one and then copying the contents into that one? Or is there a way to copy a file over an existing file?

更新(2012年8月30日):

Update (Aug. 30, 2012):

按照下面的建议的解决方案,我不能去工作:

As per the suggested solution below, which I cannot get to work:


  • 我有一个名为shoppinglists.csv文件

  • 然后,我创建了一个名为shoppinglists.tmp新的文件,并从shoppinglists.csv的内容,一些新的项目复制到。该shoppinglist.tmp文件是则shoppinglists.csv文件的新版本

  • 然后我删除旧文件shoppinglists.csv

  • 然后,我需要重命名shoppinglists.tmp文件shoppinglists.csv

我试过这样:

ctx.deleteFile("shoppinglists.csv");           <--- delete the old file
File oldfile = new File("shoppinglists.tmp");
File newfile = new File("shoppinglists.csv");
oldfile.renameTo(newfile);

但是,这是行不通的。 DELETEFILE()之后,仅此而已发生,我留下了新的shoppinglists.tmp文件。

However, this doesn't work. After deleteFile(), nothing more happens, and I'm left with the new shoppinglists.tmp file.

我是什么失踪?

注:有在LogCat中没有任何错误或任何

NB: There are no errors or anything in LogCat.

推荐答案

renameTO()没有在我的环境(Eclipse的靛蓝,AVD与Android版本2.3)工作。解决的办法是alltogeher跳过临时文件的方法,因为它似乎并不可能在任何合理的时间框架来解决

renameTO() doesn't work in my environment (Eclipse Indigo, AVD with android version 2.3). The solution is to skip the temporary file method alltogeher, since it doesn't seem to be possible to solve in any reasonable time frame.

这篇关于在内部存储重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆