How do I get file extension in Java without using that silly lastIndexOf('.')
etc.?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
With guava
Files.getFileExtension("some.txt")
Do not want to include external libraries? Use regular expressions:
That's probably the easiest way (also note that depending on the context, it's not necessarily correct, e.g. ".tar.gz").
You could also
split
the string based on the.
character and take the last piece, but that seems just as difficult.Is there a particular reason why you're trying to avoid
substring
andlastIndexOf
?The apache Commons library has
FilenameUtils.getExtension()
.You can look over the source starting here, and FilenameUtils.
At least look over their implementation. It's pretty simple, they handle dir.ext/file correctly, and to handle something like file.tar.gz you'll need a special case if you want to extract .tar.gz rather than just .gz.