【发布时间】:2014-01-23 09:02:00
【问题描述】:
我需要为 android 应用程序重命名文件名。我在 onListItemClick() 中编写了代码,但它不起作用。我也没有任何异常。这是代码。这里有什么错误?
File oldFile = new File(path.get(position));
String oldFileName=path.get(position);
Toast.makeText(this, path.get(position), Toast.LENGTH_LONG).show();
String segments[]=oldFileName.split("/");
String fileName=segments[segments.length-1];
String newFileName=oldFileName.replace(fileName, "log_B.txt");
File newFile=new File(newFileName);
if(!oldFile.isDirectory()){
System.out.println("File Name is:"+fileName);
try{
if(oldFile.renameTo(newFile)){
System.out.println("File renamed successfull !");
}else{
System.out.println("File renamed operation failed");
}
}catch(Exception ex){
System.out.println("Exception :"+ex.getMessage());
}
}else{
System.out.println("Folder Name is :"+fileName);
}
【问题讨论】:
-
让我们看看你得到的输出。
-
我得到“文件重命名操作失败”并且没有得到任何异常。我也尝试通过将路径指定为“Environment.getExternalStorageDirectory()”来做到这一点。但仍然得到相同的输出。
标签: java android file rename file-rename