All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tldgen.util.ClasspathFileUtils Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package org.tldgen.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

public class ClasspathFileUtils {

	/** Use only static methods. */
	private ClasspathFileUtils() {}
	
	/**
	 * Read a file from the classpath and return its contents
	 * @param location the path relative to the classpath
	 * @return the file contents
	 */
	public static String readContents(String location) {
		InputStream inputStream = null;
		try {
			inputStream = getInputStream(location);
			return IOUtils.toString(inputStream);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
	}
	
	public static InputStream getInputStream(String location) {
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(location);
		if (inputStream == null) {
			throw new RuntimeException("File not found in classpath: " + location);
		}
		return inputStream;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy