Java ClassLoader Load getFileContents(String filename)

Here you can find the source of getFileContents(String filename)

Description

get File Contents

License

Open Source License

Declaration

public static String getFileContents(String filename) 

Method Source Code


//package com.java2s;
/*/*from  www. j a va  2  s  .  c o m*/
 *  Copyright IBM Corp. 2011
 * 
 * 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.IOException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    public static String getFileContents(String filename) {
        StringBuffer sb = new StringBuffer();
        String p = "xsp/extlib/designer/junit/resources/" + filename; // $NON-NLS-1$

        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

        int ch;
        InputStream in = null;
        try {
            URL url = contextClassLoader.getResource(p);
            in = url.openStream();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        if (null == in)
            return null;

        try {
            while ((ch = in.read()) != -1)
                sb.append((char) ch);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
            }
        }

        return sb.toString();
    }
}

Related

  1. getContentFromFile(String fileName, Object context)
  2. getDirectories(String path)
  3. getFile(final String filename)
  4. getFile(final String name)
  5. getFile(Object obj, String filename)
  6. getFileContents(String path)
  7. getFileFromPath(Object obj, String fileName)
  8. getFileFromPool(String path)
  9. getFileFullPath(String file)