Android Path Create getUniqueFilePath(String filePath)

Here you can find the source of getUniqueFilePath(String filePath)

Description

get the unique file path by add a serial number.

Parameter

Parameter Description
filePath a parameter

Declaration

public static String getUniqueFilePath(String filePath) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**//from   w w w .  j a  v a2 s  . c o  m
     * get the unique file path by add a serial number. e.g. directory[1],
     * file[1].jpg
     * @param filePath
     * @return
     */
    public static String getUniqueFilePath(String filePath) {
        int id = 1;
        File file = new File(filePath);
        while (file.exists()) {
            if (file.isDirectory()) {
                if (id == 1)
                    filePath += "[" + (id++) + "]";
                else
                    filePath = filePath.substring(0,
                            filePath.lastIndexOf("[") + 1)
                            + (id++) + "]";
            } else {
                int pointIndex = filePath.indexOf(".");
                String suffix = null;
                if (pointIndex != -1) {
                    suffix = filePath.substring(pointIndex);
                    filePath = filePath.substring(0, pointIndex);
                }
                if (id == 1)
                    filePath += "[" + (id++) + "]";
                else
                    filePath = filePath.substring(0,
                            filePath.lastIndexOf("[") + 1)
                            + (id++) + "]";
                filePath += suffix;
            }
            file = new File(filePath);
        }
        return filePath;
    }
}

Related

  1. getDirName(String dir)
  2. getDownloadPath()
  3. getFilePath(String path)
  4. getPath(String fullFileName)
  5. getProjectAbsolutePath()
  6. getFolderPathNameYearAndMonthSubDirectoryDay()
  7. getNameDelLastPath(String fileName)
  8. getPath(String path, String parent)
  9. getRealPath(String name)