Here you can find the source of getUniqueFilePath(String filePath)
Parameter | Description |
---|---|
filePath | a parameter |
public static String getUniqueFilePath(String filePath)
//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; } }