Android Path Check isLikelyFilePath(String s)

Here you can find the source of isLikelyFilePath(String s)

Description

Return whether or not the provided string is possibly a valid file path.

License

Apache License

Parameter

Parameter Description
s The string to test

Return

true if the string is likely a valid file path; false otherwise.

Declaration

public static boolean isLikelyFilePath(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 Geoscience Australia/*from w  ww.  j  a  va  2 s .c  om*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.regex.Pattern;

public class Main {
    private static final String FILE_PATH_PATTERN = "(file://)?[a-zA-Z]:[/\\\\]([^/\\\\]+[/\\\\]?)+";

    /**
     * Return whether or not the provided string is possibly a valid file path.
     * <p/>
     * Note that this method is purely a pattern-based heuristic and doesn't
     * attempt to query the file system for a file's existence.
     * 
     * @param s
     *            The string to test
     * 
     * @return <code>true</code> if the string is likely a valid file path;
     *         <code>false</code> otherwise.
     */
    public static boolean isLikelyFilePath(String s) {
        if (s == null || s.trim().length() == 0) {
            return false;
        }

        return Pattern.matches(FILE_PATH_PATTERN, s);
    }
}

Related

  1. getUserCurrentDir()
  2. isAbsolutePath(String path)