Java Utililty Methods Path File Read nio

List of utility methods to do Path File Read nio

Description

The list of methods to do Path File Read nio are organized into topic(s).

Method

StringbreadcrumbPath(String urlPrefix, String path, char sep)
Convenience method for breadcrumbPath(urlPrefix, path, sep, "", false) .
return breadcrumbPath(urlPrefix, path, sep, "", false);
PathcheckReadableDirectory(final Path directory)
check Readable Directory
requireNonNull(directory, "No directory provided");
if (!(Files.isReadable(directory) && Files.isDirectory(directory))) {
    throw new IllegalArgumentException(String.format("Directory is not readable, %s", directory));
return directory;
BufferedReadergetReaderForFile(final Path file)
get Reader For File
final String filename = file.getFileName().toString().toLowerCase();
if (filename.endsWith(".gz")) {
    return new BufferedReader(new InputStreamReader(new GZIPInputStream(Files.newInputStream(file))));
return Files.newBufferedReader(file);
InputStreamgetResourceAsStream(String resourcePath, ClassLoader classLoader, Class clazz, String resourceDesc, Consumer logger)
Get the InputStream input stream to the resource given by the supplied path.
if (resourcePath == null)
    throw new IllegalArgumentException("resourcePath may not be null");
if (resourceDesc == null && logger != null)
    resourceDesc = resourcePath;
InputStream result = null;
if (result == null) {
    try {
        Path filePath = FileSystems.getDefault().getPath(resourcePath).toAbsolutePath();
...
URLClassLoadergetTestClassPathURLClassLoader(ClassLoader parent)
get Test Class Path URL Class Loader
URL[] urls = Arrays.stream(TEST_CLASS_PATH.split(File.pathSeparator)).map(Paths::get).map(Path::toUri)
        .map(x -> {
            try {
                return x.toURL();
            } catch (MalformedURLException ex) {
                throw new Error(
                        "Test issue. JTREG property" + " 'test.class.path'" + " is not defined correctly",
                        ex);
...
Propertiesload(Path p, PrintStream errorStream, String propertyInfo)
load
try {
    FileInputStream fis = new FileInputStream(p.toFile());
    return loadAndClose(fis, errorStream, propertyInfo);
} catch (FileNotFoundException e) {
    errorStream.println("Could not find property file " + propertyInfo);
    return new Properties();
StringloadFile(final Path filePath)
load File
if (!isExists(filePath) || !isFile(filePath)) {
    throw new IllegalArgumentException("The given path doesn't point to an existing file");
StringBuilder strBuilder = new StringBuilder();
try (Scanner scanner = new Scanner(filePath, StandardCharsets.UTF_8.name())) {
    while (scanner.hasNextLine()) {
        strBuilder.append(scanner.nextLine());
        strBuilder.append('\n');
...
StringloadFile(final Path p)
load File
Scanner in = null;
try {
    in = new Scanner(p.toFile());
} catch (FileNotFoundException e) {
    e.printStackTrace();
StringBuffer buffer = new StringBuffer();
while (in.hasNextLine()) {
...
PropertiesloadFile(Path file)
load File
Properties props = new Properties();
try (InputStream in = Files.newInputStream(file)) {
    props.load(in);
return props;
FileloadFile(String path)
load File
if (path == null) {
    path = "/";
File f = new File(path);
if (f.exists()) {
    return f.getAbsoluteFile();
if (!path.startsWith("/")) {
...