android系统中重命名文件扩展名 [英] renaming file extensions in android
问题描述
我能够改变文件扩展名,例如,从.MP4到.xmp4但不是改扩建,我只是想添加一个。例如文件名之前 mikey.jpg 到 .mikey.jpg 。我该怎么做呢?
I am able to change file extensions, for example, from ".mp4" to ".xmp4" but instead of changing extension, i simply want to add a "." before a file name for example "mikey.jpg" to ".mikey.jpg". how do i do that?
public static final String[] TARGET_EXTENSIONS = { "mp4", "mp3", "mp55", "other" };
public void walkdir(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
String fPath = listFile[i].getPath();
for (String ext : TARGET_EXTENSIONS) {
fPath = fPath.replace("." + ext, ".x" + ext);
}
listFile[i].renameTo(new File(fPath));
}
}
}
}
下面是完整的code
here is the full code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (LinearLayout) findViewById(R.id.view);
// getting SDcard root path
File dir = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath());
walkdir(dir);
}
public static final String[] TARGET_EXTENSIONS = { "mp4", "mp3", "avi", "other" };
public void walkdir(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
String fPath = listFile[i].getPath();
for (String ext : TARGET_EXTENSIONS) {
fPath = fPath.replace("." + ext, ".x" + ext);
}
listFile[i].renameTo(new File(fPath));
}
}
}
}
}
推荐答案
首先,你做的字符串文件名=的文件列表[I] .getName();
,这应该给你名字,然后你做字符串FULLPATH =的文件列表[I] .getAbsolutePath();
来获得完整的路径,然后你做 INT indexOfFileNameStart = FULLPATH .lastIndexOf(文件名)
,那么你从完整得到一个字符串生成器实例,像这样 StringBuilder的SB =新的StringBuilder(完整路径);
,现在给你打电话某人 sb.insert insert方法(indexOfFileNameStart,。)
,现在某人应该有你想要的字符串,只需将其构建为字符串 SB的ToString()
first you do String fileName = listFile[i].getName();
, which should give you the name, next you do String fullPath = listFile[i].getAbsolutePath();
to get the full path, then you do int indexOfFileNameStart = fullPath.lastIndexOf(fileName)
, then you get a string builder instance from fullPath like so StringBuilder sb = new StringBuilder(fullPath);
, now you call the insert method on sb sb.insert(indexOfFileNameStart, ".")
, now sb should have the string you desire, just construct it to string sb.toString()
在伊利诺伊州code添加此
Ill add this in code
private String putDotBeforeFileName(File file) {
String fileName = file.getName();
String fullPath = file.getAbsolutePath();
int indexOfFileNameStart = fullPath.lastIndexOf(fileName);
StringBuilder sb = new StringBuilder(fullPath);
sb.insert(indexOfFileNameStart, ".");
String myRequiredFileName = sb.toString();
file.renameTo(new File(myRequiredFileName));
return myRequiredFileName;
}
修改
这是你如何可以在code使用上面的方法
This is how you can use the above method in your code
public void walkdir(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
String fPath = listFile[i].getPath();
for (String ext : TARGET_EXTENSIONS) {
if(fPath.endsWith(ext)) {
putDotBeforeFileName(listFile[i]);
}
}
}
}
}
}
这篇关于android系统中重命名文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!