【问题标题】:Rename folder in SD card重命名 SD 卡中的文件夹
【发布时间】:2011-08-16 22:28:25
【问题描述】:

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

在我的应用程序中,我在 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) 
{
}

有什么想法吗?

【问题讨论】:

    标签: android directory rename


    【解决方案1】:

    您是否尝试过在 File 中使用 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 感谢您指出这一点,我已经更新了示例网址
    【解决方案2】:
    final File F=new File("youroldpath");  
    String newname="newname";  
    File newfile=new File(F.getParent(),newname);  
    F.renameTo(newfile);
    

    【讨论】:

      【解决方案3】:
      File file = new File("your old file name");
      File file2 = new File("your new file name");
      boolean success = file.renameTo(file2);
      

      【讨论】:

        猜你喜欢
        • 2012-02-22
        • 1970-01-01
        • 2011-08-08
        • 2011-10-18
        • 2012-09-20
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 2018-05-18
        相关资源
        最近更新 更多