SD卡中重命名文件夹

4

这应该是一个非常简单的任务,但我已经困扰了一段时间,没有成功...

在我的应用程序中,我在SD卡上创建一个文件夹,其中存储临时jpg文件。由于我不希望我的应用程序在浏览手机图片时显示那些临时文件,因此我试图将该文件夹隐藏起来。因此,在创建目录之后,我尝试将其重命名,如下所示:

String tmppath="/sdcard/myapp/tmp";
try
{
//this creates a directory named TMP -->OK!
 File f=new File(tmppath); 
  if(!f.isDirectory())  
   f.mkdirs();  

//this was supposed to rename the directory to .TMP, but isn't working...

Process process=Runtime.getRuntime().exec("mv "+tmppath +" /sdcard/myapp/.tmp/");
process.waitFor();
}
catch(SecurityException e)
{
}
catch(IOException e)
{
} 
catch (InterruptedException e) 
{
}

有什么想法吗?
3个回答

15
File file = new File("your old file name");
File file2 = new File("your new file name");
boolean success = file.renameTo(file2);

6
final File F=new File("youroldpath");  
String newname="newname";  
File newfile=new File(F.getParent(),newname);  
F.renameTo(newfile);

2
你尝试过在文件中使用renameTo方法吗?这里有一个重命名文件或文件夹的示例Here
package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {

      File f = null;
      File f1 = null;
      boolean bool = false;

      try{      
         // create new File objects
         f = new File("C:/test.txt");
         f1 = new File("C:/testABC.txt");

         // rename file
         bool = f.renameTo(f1);

         // print
         System.out.print("File renamed? "+bool);

      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}

@Juan 感谢您指出这一点,我已经更新了示例URL。 - Hakan Ozbay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接