String (@NotNull String path) { String[] split = path.split("/"); if (split.length > 3) { // split[0] is empty, because the path starts from / return String.format("/%s/.../%s/%s", split[1], split[split.length - 2], split[split.length - 1]); } return path; }