mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-15 12:29:26 +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:
parent
232f434ca8
commit
9e4243bc7e
@ -241,15 +241,44 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasExtension(File file, List<String> extensions) {
|
public static boolean hasExtension(File file, List<String> extensions) {
|
||||||
String pieces[] = file.getName().split("\\.");
|
String extension = splitFilename(file).extension;
|
||||||
if (pieces.length < 2) {
|
return extensions.contains(extension.toLowerCase());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String extension = pieces[pieces.length - 1];
|
/**
|
||||||
|
* The result of a splitFilename call.
|
||||||
|
*/
|
||||||
|
public static class SplitFile {
|
||||||
|
public SplitFile(String basename, String extension) {
|
||||||
|
this.basename = basename;
|
||||||
|
this.extension = extension;
|
||||||
|
}
|
||||||
|
|
||||||
return extensions.contains(extension.toLowerCase());
|
public String basename;
|
||||||
|
public String extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user