简体   繁体   English

使用Java在Android中重命名文件的替代方法

[英]Alternate method for renaming files in android using java

Is there another way to rename a file without using using File.renameTo method? 是否有另一种不使用File.renameTo方法来重命名文件的方法? also, I am new to android, so asking this question : Is there a way to rename a file without making the code to open that file? 另外,我是android的新手,所以问这个问题:是否可以在不编写代码的情况下重命名文件?

I want to just rename the file, not open it or copy its contents. 我只想重命名文件,而不是打开它或复制其内容。

Try this: Renaming the file by moving it to a new name. 尝试以下操作:通过将文件移动到新名称来重命名文件。 (FileUtils is from Apache Commons IO lib) (FileUtils来自Apache Commons IO库)

  String newFilePath = oldFile.getAbsolutePath().replace(oldFile.getName(), "") + newName;
  File newFile = new File(newFilePath);

  try {
    FileUtils.moveFile(oldFile, newFile);
  } catch (IOException e) {
    e.printStackTrace();
  }

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

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