get Resource Reader - Java File Path IO

Java examples for File Path IO:BufferedReader

Description

get Resource Reader

Demo Code


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

public class Main {
  public static void main(String[] argv) throws IOException {
    Object caller = "java2s.com";
    String resourceName = "java2s.com";
    System.out.println(getResourceReader(caller, resourceName));
  }// ww w. ja  v  a  2 s .  co m

  public static Reader getResourceReader(final Object caller, final String resourceName) throws IOException {
    BufferedReader in = null;
    if (caller == null) {
      throw new IllegalArgumentException("caller is null");
    }
    try {
      final InputStream stream = caller.getClass().getClassLoader().getResourceAsStream(resourceName);
      if (stream == null) {
        throw new java.lang.IllegalArgumentException("irresolvable resource " + resourceName);
      }
      in = new BufferedReader(new InputStreamReader(stream, "8859_1"));

    } catch (final java.io.IOException i) {
      throw new java.io.IOException("Failed to load resource: '" + resourceName + "'\n" + i.getMessage());
    }
    return in;
  }
}

Related Tutorials