Description
Lists URIs relative to the given Path .
License
Open Source License
Parameter
Parameter | Description |
---|
content | The path within which to list URIs. |
Exception
Parameter | Description |
---|
IOException | If a filesystem error occurs. |
Return
A list of URIs for files only (not directories)>
Declaration
public static List<String> listUris(Path content) throws IOException
Method Source Code
//package com.java2s;
//License from project: Open Source License
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class Main {
/**/* ww w . j av a2 s.c o m*/
* Lists URIs relative to the given {@link Path}.
*
* @param content The path within which to list URIs.
* @return A list of URIs for files only (not directories)>
* @throws IOException If a filesystem error occurs.
*/
public static List<String> listUris(Path content) throws IOException {
List<Path> paths = listFiles(content);
List<String> result = new ArrayList<>();
for (Path path : paths) {
String uri = toUri(path, content);
result.add(uri);
}
return result;
}
static List<Path> listFiles(Path path) throws IOException {
final List<Path> result = new ArrayList<>();
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
result.add(file);
return FileVisitResult.CONTINUE;
}
});
return result;
}
/**
* @param path The path to be rendered as a URI
* @param root The path the URI should start from.
* @return If path is contained within root, a URI relative to root (with leading slash).
* @throws IOException If a filesystem error occurs.
*/
public static String toUri(Path path, Path root) throws IOException {
String result = null;
if (isContained(path, root)) {
Path relative = root.relativize(path);
result = setLeadingSlash(relative.toString());
}
return result;
}
/**
* Inspired by <a href="http://stackoverflow.com/questions/18227634/check-if-file-is-in-subdirectory"
* >http://stackoverflow.com/questions/18227634/check-if-file-is-in-subdirectory</a>
*
* @param contained The path to be checked. This does not need to exist.
* @param container The parent directory. This must exist on the filesystem.
* @return If <code>contained</code> is a subfolder of <code>container</code>, true.
* @throws IOException If an error occurs or, potentially, if either path does not exist.
*/
public static boolean isContained(Path contained, Path container) throws IOException {
Path current = contained.normalize();
// Iterate up the path to see if we find the container path:
while (current != null) {
if (Files.isSameFile(container, current)) {
return true;
}
current = current.getParent();
}
// If we didn't find the container path
// amongst the parents of the contained path,
// this path is not contained:
return false;
}
/**
* Ensures the given string has a leading slash. This is useful for ensuring URIs always have a leading slash.
*
* @param string The value to be altered if necessary.
* @return If the given string has a leading slash, the string is returned, otherwise a string with a slash prepended.
*/
public static String setLeadingSlash(String string) {
String result = string;
if (result != null && !result.startsWith("/")) {
result = "/" + result;
}
return result;
}
}
Related
- fileContent(final String filePath)
- getContent(Path path)
- getFileContents(final String absFilePath)
- getFileContents(Path file)
- getFileContents(String path)
- readContent(Path file)
- saveByteArrayToFile(byte[] content, Path targetPath)
- saveFile(String workspacePath, byte[] content)
- slurpFileContent(Path path)