android,如何重命名文件?
在我的应用程序中,我需要录制视频。在开始录制之前,我将为它指定一个名称和目录。录制完成后,用户可以重命名其文件。我写了下面的代码,但似乎不起作用 当用户输入文件名并单击按钮时,我将执行以下操作:android,如何重命名文件?,android,file-rename,Android,File Rename,在我的应用程序中,我需要录制视频。在开始录制之前,我将为它指定一个名称和目录。录制完成后,用户可以重命名其文件。我写了下面的代码,但似乎不起作用 当用户输入文件名并单击按钮时,我将执行以下操作: private void setFileName(String text) { String currentFileName = videoURI.substring(videoURI.lastIndexOf("/"), videoURI.length());
private void setFileName(String text) {
String currentFileName = videoURI.substring(videoURI.lastIndexOf("/"), videoURI.length());
currentFileName = currentFileName.substring(1);
Log.i("Current file name", currentFileName);
File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), MEDIA_NAME);
File from = new File(directory, "currentFileName");
File to = new File(directory, text.trim() + ".mp4");
from.renameTo(to);
Log.i("Directory is", directory.toString());
Log.i("Default path is", videoURI.toString());
Log.i("From path is", from.toString());
Log.i("To path is", to.toString());
}
Text:用户输入的名称。
当前文件名:是我在录制前指定的名称
媒体名称:文件夹的名称
Logcat显示:
05-03 11:56:37.295: I/Current file name(12866): Mania-Karaoke_20120503_115528.mp4
05-03 11:56:37.295: I/Directory is(12866): /mnt/sdcard/Movies/Mania-Karaoke
05-03 11:56:37.295: I/Default path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/Mania-Karaoke_20120503_115528.mp4
05-03 11:56:37.295: I/From path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/currentFileName
05-03 11:56:37.295: I/To path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/hesam.mp4
如有任何建议,将不胜感激。请为目标文件对象提供不同的文件名
// Copy the source file to target file.
// In case the dst file does not exist, it is created
void copy(File source, File target) throws IOException {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
您应该检查目录是否存在
File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), MEDIA_NAME);
if(!directory.exist()){
directory.mkdirs();
}
在您的代码中: 难道不是:
File from=新文件(目录,当前文件名)代码>
而不是
filefrom=新文件(目录,“currentFileName”)代码>
为了安全,
使用文件.renameTo()。但是在重命名之前检查目录是否存在
File dir = Environment.getExternalStorageDirectory();
if(dir.exists()){
File from = new File(dir,"from.mp4");
File to = new File(dir,"to.mp4");
if(from.exists())
from.renameTo(to);
}
参考:
问题出在这一行
File from = new File(directory, "currentFileName");
这里的currentFileName
实际上是一个字符串,您不必使用“
这样试试看
File from = new File(directory, currentFileName );
^ ^ //You dont need quotes
工作示例
File oldFile = new File("your old file name");
File latestname = new File("your new file name");
boolean success = oldFile .renameTo(latestname );
if(success)
System.out.println("file is renamed..");
使用此方法重命名文件。来自
的文件将重命名为
至
private boolean rename(File from, File to) {
return from.getParentFile().exists() && from.exists() && from.renameTo(to);
}
示例代码:
public class MainActivity extends Activity {
private static final String TAG = "YOUR_TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File currentFile = new File("/sdcard/currentFile.txt");
File newFile = new File("/sdcard/newFile.txt");
if (rename(currentFile, newFile)) {
//Success
Log.i(TAG, "Success");
} else {
//Fail
Log.i(TAG, "Fail");
}
}
private boolean rename(File from, File to) {
return from.getParentFile().exists() && from.exists() && from.renameTo(to);
}
}
这就是我最后使用的。它通过在文件名中添加一个整数来处理存在同名文件的情况
@NonNull
private static File renameFile(@NonNull File from,
@NonNull String toPrefix,
@NonNull String toSuffix) {
File directory = from.getParentFile();
if (!directory.exists()) {
if (directory.mkdir()) {
Log.v(LOG_TAG, "Created directory " + directory.getAbsolutePath());
}
}
File newFile = new File(directory, toPrefix + toSuffix);
for (int i = 1; newFile.exists() && i < Integer.MAX_VALUE; i++) {
newFile = new File(directory, toPrefix + '(' + i + ')' + toSuffix);
}
if (!from.renameTo(newFile)) {
Log.w(LOG_TAG, "Couldn't rename file to " + newFile.getAbsolutePath());
return from;
}
return newFile;
}
@NonNull
私有静态文件重命名文件(@NonNull File from,
@非空字符串toPrefix,
@非空字符串(toSuffix){
File directory=from.getParentFile();
如果(!directory.exists()){
if(directory.mkdir()){
Log.v(Log_标记,“Created directory”+directory.getAbsolutePath());
}
}
File newFile=新文件(目录,toPrefix+toSuffix);
对于(int i=1;newFile.exists()&&i
哦,我的天!我犯了多么愚蠢的错误!!!!谢谢亲爱的桑杰。现在,在我改变了它之后,它工作得很好。@Hesam有时这样愚蠢的错误会占用我们所有的时间。:)干杯。。快乐的编码:)哈哈,每个人都会犯错,但这真是一个有趣的错误,休息一下,直到你完全充满活力后再回来。我会继续来的每隔两个月返回此解决方案。感谢您提供的简明解决方案。@Igor此问题是针对Android操作系统的操作系统特定问题。newFile声明中缺少“=”。
@NonNull
private static File renameFile(@NonNull File from,
@NonNull String toPrefix,
@NonNull String toSuffix) {
File directory = from.getParentFile();
if (!directory.exists()) {
if (directory.mkdir()) {
Log.v(LOG_TAG, "Created directory " + directory.getAbsolutePath());
}
}
File newFile = new File(directory, toPrefix + toSuffix);
for (int i = 1; newFile.exists() && i < Integer.MAX_VALUE; i++) {
newFile = new File(directory, toPrefix + '(' + i + ')' + toSuffix);
}
if (!from.renameTo(newFile)) {
Log.w(LOG_TAG, "Couldn't rename file to " + newFile.getAbsolutePath());
return from;
}
return newFile;
}
public void renameFile(File file,String suffix) {
String ext = FilenameUtils.getExtension(file.getAbsolutePath());
File dir = file.getParentFile();
if(dir.exists()) {
File from = new File(dir, file.getName());
String name = file.getName();
int pos = name.lastIndexOf(".");
if (pos > 0)
name = name.substring(0, pos);
File to = new File(dir, name + suffix + "." + ext);
if(from.exists())
from.renameTo(to);
}
}