Skip to content

Commit

Permalink
Improve PathUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Jan 2, 2024
1 parent eedf263 commit c2dcaf2
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
package com.kohlschutter.util;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Some {@link Path}-related helper methods.
Expand Down Expand Up @@ -120,4 +123,21 @@ public static Path partialRealpath(Path p) {
return newPath;
}
}

/**
* Converts the given URL to a Path, if possible.
*
* @param url The URL to convert to.
* @return The Path, or {@code null} if not convertible.
*/
public static Path toPathIfPossible(URL url) {
if (url == null) {
return null;
}
try {
return Paths.get(url.toURI());
} catch (URISyntaxException e) {
return null;
}
}
}

0 comments on commit c2dcaf2

Please sign in to comment.