| Refresh | Home EGTry.com

read file from java classpath


read text file from java classpath

package io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadFileFromClasspath {


	public static void main(String[] args)throws Exception {

		System.out.println(getContent("/simple.xml"));
	}

	public static String getContent(String resourceName) throws IOException{
		InputStream in = ReadFileFromClasspath.class.getResourceAsStream(resourceName);
		BufferedReader reader=new BufferedReader(new InputStreamReader(in));
		StringBuilder builder=new StringBuilder();
		String line=null;
		while( (line=reader.readLine()) !=null) {
			builder.append(line+"\n");
		}
		in.close();
		return builder.toString();
	}
}