简体   繁体   English

为什么我的文件不重命名?

[英]Why won't my file rename itself?

I am working on an android app and I want to rename a file. 我正在开发一个android应用,我想重命名文件。 The problem is that it is not renaming: 问题在于它没有重命名:

 File f = adapter.getItem(i);
 File file = new File(f.getAbsolutePath(), "helloworld");
 if (f.renameTo(file)) {
 Toast.makeText(getActivity(), "done", Toast.LENGTH_LONG).show();
 }

Solution BIg Thanks to @SD(see comments) 解决方案BIg感谢@SD(请参阅评论)

File f = adapter.getItem(i);
     File file = new File(f.getParent(), "helloworld");
     if (f.renameTo(file)) {
     Toast.makeText(getActivity(), "done", Toast.LENGTH_LONG).show();
     }

I think the issue is that: 我认为问题在于:

File f = adapter.getItem(i);

Gives use some File f , say where f cooresponds to say: user2351234/Desktop . 给出一些File f ,说f对应说: user2351234/Desktop Then, you do: 然后,您执行以下操作:

 File file = new File(f.getAbsolutePath(), "helloworld");

Which says to make a File file , where file cooresponds to: user2351234/Desktop/helloworld . 上面说要制作一个File file ,该file于: user2351234/Desktop/helloworld Next, you call: 接下来,您致电:

f.renameTo(file)

which attempts to rename f , user2351234/Desktop to user2351234/Desktop/helloworld , which doesn't make sense since in order for user2351234/Desktop/helloworld to exist, user2351234/Desktop would have to exist, but by virtue of the operation it would no longer exist. 它尝试将fuser2351234/Desktop重命名为user2351234/Desktop/helloworld ,这没有意义,因为user2351234/Desktop/helloworld存在, user2351234/Desktop必须存在,但是通过操作,它将不复存在。

My hypothesis may not be the reason why, but from Why doesn't File.renameTo(…) create sub-directories of destination? 我的假设可能不是原因,而是从File.renameTo(...)为什么不创建目的地的子目录? , apparently renameTo will return false if the sub-directory does not exist. ,显然,如果子目录不存在,则renameTo 返回false

If you want to just change the name of the file, do this: 如果只想更改文件名,请执行以下操作:

File f = adapter.getItem(i);
String file = f.getAbsolutePath() + "helloworld";
f = new File(file);

EDIT: 编辑:
My proposed solution should work, but if my hypothesis about why your way does not work is incorrect, you may want to see this answer from Reliable File.renameTo() alternative on Windows? 我建议的解决方案应该可以使用,但是如果我关于您的方法为何无法使用的假设不正确,则您可能希望从Windows上的Reliable File.renameTo()替代品中看到此答案

Question 1: Do you see an exception or does it return false? 问题1:您看到异常还是返回false?

Question 2: Did you give permission for the app to write to the SD card? 问题2:您是否已允许该应用写入SD卡? (I'm assuming that's where this file lies). (我假设这是该文件所在的位置)。 The permission to add is" 添加权限为“

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This posting: How to rename a file on sdcard with Android application? 这篇文章: 如何使用Android应用程序重命名sdcard上的文件? seems to answer a similar question. 似乎回答了类似的问题。

use this code. 使用此代码。

File sdcard = Environment.getExternalStorageDirectory()+ "/nameoffile.ext" ;
File from = new File(sdcard,"originalname.ext");
File to = new File(sdcard,"newname.ext");
from.renameTo(to);

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

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