Java ClassPath readFromClassPath(String filePath)

Here you can find the source of readFromClassPath(String filePath)

Description

read From Class Path

License

Apache License

Parameter

Parameter Description
filePath name of file to open. The file can reside anywhere in the classpath

Declaration

public static BufferedReader readFromClassPath(String filePath) throws java.io.IOException 

Method Source Code

//package com.java2s;
/* //from  w  ww  .  ja  va2  s .c om
 * Copyright 2016 Lutz Fischer <l.fischer@ed.ac.uk>.
 *
 * 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.io.*;
import java.net.URL;

import java.util.regex.*;

public class Main {
    /**
     * @param filePath name of file to open. The file can reside anywhere in the
     * classpath
     */
    public static BufferedReader readFromClassPath(String filePath) throws java.io.IOException {
        String path = filePath;
        System.err.println("!!!reading config!!!");
        System.err.println("from : " + filePath);
        StringBuffer fileData = new StringBuffer(1000);
        URL res = Object.class.getResource(path);

        while (res == null && path.contains(".")) {

            path = path.replaceFirst("\\.", Matcher.quoteReplacement(File.separator));
            res = Object.class.getResource(path);
        }

        if (res == null) {
            path = filePath;
            while (res == null && path.contains(".")) {

                path = path.replaceFirst("\\.", "/");
                res = Object.class.getResource(path);
            }
        }

        InputStream is = Object.class.getResourceAsStream(path);
        return new BufferedReader(new InputStreamReader(is));
    }
}

Related

  1. parseJavaClassPath()
  2. printClassPath()
  3. printClassPath(boolean single)
  4. printJVMClassPath()
  5. readFileFromClasspath(String fileName)
  6. readStringFromClasspath(String path, Class c)
  7. toClassLoaderWithDefaultParent(Collection classPaths)
  8. toClasspathString(ClassLoader cl)
  9. toFiles(String classPath)