Java Resource Path Get getStringFromResource(Class clazz, String path)

Here you can find the source of getStringFromResource(Class clazz, String path)

Description

get String From Resource

License

Apache License

Declaration

public static String getStringFromResource(Class<?> clazz, String path) 

Method Source Code

//package com.java2s;
/*/*from   w ww .  j  ava  2 s . c o  m*/
 * Copyright 2004,2004 The Apache Software Foundation.
 * 
 * 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;

public class Main {
    public static String getStringFromResource(Class<?> clazz, String path) {
        if (clazz == null)
            throw new IllegalArgumentException("clazz is null.");
        if (path == null)
            throw new IllegalArgumentException("path is null.");
        StringWriter swOut = new StringWriter();

        try {
            URL resource = clazz.getResource(path);
            if (resource == null)
                throw new IllegalArgumentException("can't get resource");
            InputStreamReader reader = new InputStreamReader(
                    resource.openStream());
            BufferedReader bufIn = new BufferedReader(reader);
            PrintWriter pwOut = new PrintWriter(swOut);
            String tempLine;

            while ((tempLine = bufIn.readLine()) != null) {
                pwOut.println(tempLine);
            }

            pwOut.flush();
        } catch (IOException e) {

        }

        return swOut.toString();
    }
}

Related

  1. getResources(ClassLoader cl, String resourcePath)
  2. getResources(String path, ClassLoader loader)
  3. getResources(String resourcePath, Class caller)
  4. getResourceStream(final String bundle, final String path)
  5. getResourceStream(String path)
  6. getSystemResource(String path)
  7. getSystemResource(String path)
  8. loadResource(final Class bundleClazz, final Class resourceTypeclazz, final String pathToFile)
  9. loadResources(ClassLoader loader, String path)