Java Path File Check nio isValidPath(String path)

Here you can find the source of isValidPath(String path)

Description

Check whether a path is valid and parsable, without the file needing to exist.

License

Open Source License

Parameter

Parameter Description
path Path to validate.

Return

True if the path is valid, false if not.

Declaration

public static boolean isValidPath(String path) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) Child-In-Time 2016. All rights reserved.                     *
 *                                                                            *
 * @author Tim Visee                                                          *
 * @author Nathan Bakhuijzen                                                  *
 * @author Timo van den Boom                                                  *
 * @author Jos van Gent                                                       *
 *                                                                            *
 * Open Source != No Copyright                                                *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software")  *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included    *
 * in all copies or substantial portions of the Software.                     *
 *                                                                            *
 * You should have received a copy of The MIT License (MIT) along with this   *
 * program. If not, see <http://opensource.org/licenses/MIT/>.                *
 ******************************************************************************/

import java.nio.file.Paths;

public class Main {
    /**/*from w  ww.j  av a 2s  . co m*/
     * Check whether a path is valid and parsable, without the file needing to exist.
     *
     * @param path Path to validate.
     *
     * @return True if the path is valid, false if not.
     */
    public static boolean isValidPath(String path) {
        // Validate the path
        try {
            // Try to parse the given path as string representation into a path instance
            Paths.get(path);

            // The path seems to be valid
            return true;

        } catch (Exception ignored) {
        }

        // The path seems to be invalid, return false
        return false;
    }
}

Related

  1. isUNC(Path inputPath)
  2. isUnix(Path path)
  3. isValidDirectory(String path)
  4. isValidFileType(Path filePath)
  5. isValidHomeDirectory(final Path path)
  6. isValidPath(String path)
  7. isValidPath(String path)
  8. isValidRootArgument(Path argument)
  9. isValidWildFlyHome(final Path path)