In my code, I would like to transform a path of the form
/a/path/to/a/file/image.jpg
to
/a/path/to/a/file/image_resized.jpg
Currently, I am using the the following code which uses FilenameUtils
from apache commons IO.
public Path resize(Path original) {
String baseName = FilenameUtils.getBaseName(original.toString());
String extension = FilenameUtils.getExtension(original.toString());
return Paths.get(original.getParent().toString(), baseName + "_resized." + extension);
}
I was wondering if some of this code could be enhanced using java 8 features, specifically:
toString()
in Paths.get(existingPath.toString(), "path/to/append");
The second part of the question is answered in Combine paths in Java
You don't need a library for such a small and simple task IMO (and no java-8 does not add support for that); and I also can't tell why a regex is out of the question
int where = input.lastIndexOf(".");
String result = input.substring(0, where) + "_resized" + input.substring(where);
System.out.println(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With