1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-13 10:29:35 +01:00

Add FileUtils.splitFilename()

This allows splitting a filename into a basename and extension.
`FileUtils.hasExtension()` is updated to use it, in favour of the
String.split-based approached it used before.
This commit is contained in:
Matthijs Kooijman 2015-12-08 16:32:45 +01:00 committed by Cristian Maglie
parent 232f434ca8
commit 9e4243bc7e

View File

@ -241,15 +241,44 @@ public class FileUtils {
}
public static boolean hasExtension(File file, List<String> extensions) {
String pieces[] = file.getName().split("\\.");
if (pieces.length < 2) {
return false;
String extension = splitFilename(file).extension;
return extensions.contains(extension.toLowerCase());
}
/**
* The result of a splitFilename call.
*/
public static class SplitFile {
public SplitFile(String basename, String extension) {
this.basename = basename;
this.extension = extension;
}
String extension = pieces[pieces.length - 1];
public String basename;
public String extension;
}
return extensions.contains(extension.toLowerCase());
/**
* Splits the given filename into a basename (everything up to the
* last dot) and an extension (everything from the last dot). The dot
* is not included in either part.
*
* If no dot is present, the entire filename is returned as the
* basename, leaving the extension empty (empty string, not null).
*/
public static SplitFile splitFilename(String filename) {
int index = filename.lastIndexOf(".");
if (index >= 0)
return new SplitFile(filename.substring(0, index), filename.substring(index + 1));
return new SplitFile(filename, "");
}
/**
* Helper function returning splitFilename(file.getName()).
*/
public static SplitFile splitFilename(File file) {
return splitFilename(file.getName());
}
/**