Here you can find the source of getStringFromResource(Class> clazz, String path)
public static String getStringFromResource(Class<?> clazz, String path)
//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(); } }