diff options
| author | Bananeweizen <Bananeweizen@gmx.de> | 2013-07-28 17:46:09 +0200 |
|---|---|---|
| committer | Bananeweizen <Bananeweizen@gmx.de> | 2013-07-28 17:49:09 +0200 |
| commit | 51e61f7b36a731ce7ed4087fbe38642687be0321 (patch) | |
| tree | 418cc9b91f0b66df0141bd0a68a370afaee03abc /main/src/cgeo/geocaching/utils/FileUtils.java | |
| parent | 77e7e33e8956fa00b6e7f43be6c51177f4b6cf5b (diff) | |
| download | cgeo-51e61f7b36a731ce7ed4087fbe38642687be0321.zip cgeo-51e61f7b36a731ce7ed4087fbe38642687be0321.tar.gz cgeo-51e61f7b36a731ce7ed4087fbe38642687be0321.tar.bz2 | |
change: make gpx export name more readable
* use only date instead of date and time
* format date using dashes
* append _2, _3, ... with further exports
Diffstat (limited to 'main/src/cgeo/geocaching/utils/FileUtils.java')
| -rw-r--r-- | main/src/cgeo/geocaching/utils/FileUtils.java | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/utils/FileUtils.java b/main/src/cgeo/geocaching/utils/FileUtils.java index 5ab8fcc..9951af1 100644 --- a/main/src/cgeo/geocaching/utils/FileUtils.java +++ b/main/src/cgeo/geocaching/utils/FileUtils.java @@ -1,6 +1,7 @@ package cgeo.geocaching.utils; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; import android.os.Handler; import android.os.Message; @@ -65,4 +66,29 @@ public final class FileUtils { public abstract boolean shouldEnd(); } + + /** + * Create a unique non existing file named like the given file name. If a file with the given name already exists, + * add a number as suffix to the file name.<br> + * Example: For the file name "file.ext" this will return the first file of the list + * <ul> + * <li>file.ext</li> + * <li>file_2.ext</li> + * <li>file_3.ext</li> + * </ul> + * which does not yet exist. + */ + public static File getUniqueNamedFile(final String baseNameAndPath) { + String extension = StringUtils.substringAfterLast(baseNameAndPath, "."); + String pathName = StringUtils.substringBeforeLast(baseNameAndPath, "."); + int number = 1; + while (new File(getNumberedFileName(pathName, extension, number)).exists()) { + number++; + } + return new File(getNumberedFileName(pathName, extension, number)); + } + + private static String getNumberedFileName(String pathName, String extension, int number) { + return pathName + (number > 1 ? "_" + Integer.toString(number) : "") + "." + extension; + } } |
