在android中重命名文件扩展名

时间:2016-02-29 06:50:29

标签: android

我可以更改文件扩展名,例如,来自" .mp4" to" .xmp4"但我没有改变扩展名,只想添加一个"。"在文件名前例如" mikey.jpg "到" .mikey.jpg "。我该怎么做?

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));
            }
        }
    }
}

这是完整的代码

@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));
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

首先执行String fileName = listFile[i].getName();,它应该为您提供名称,然后执行String fullPath = listFile[i].getAbsolutePath();以获取完整路径,然后执行int indexOfFileNameStart = fullPath.lastIndexOf(fileName),然后从中获取字符串生成器实例fullPath就像这样StringBuilder sb = new StringBuilder(fullPath);,现在你在sb sb.insert(indexOfFileNameStart, ".")上调用insert方法,现在sb应该有你想要的字符串,只需将它构造成字符串sb.toString()

我在代码

中添加此内容
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;
}

修改

这是您在代码中使用上述方法的方法

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]);
                    }
                }
            }
        }
    }
}