集册 Java实例教程 获取资源阅读器

获取资源阅读器

欢马劈雪     最近更新时间:2020-01-02 10:19:05

522
获取资源阅读器


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;/**来自 N o w J a v a . c o m - 时  代  Java**/

import java.io.InputStreamReader;

import java.io.Reader;


public class Main {

  public static void main(String[] argv) throws IOException {

    Object caller = "nowjava.com";

    String resourceName = "nowjava.com";

    System.out.println(getResourceReader(caller, resourceName));

  }


  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);
      /* from 
      N o w  J a v a  .   c o m*/

      if (stream == null) {

        throw new java.lang.IllegalArgumentException("irresolvable resource " + resourceName);

      }

      in = new BufferedReader(new InputStreamReader(stream, "8859_1"));


    } 
展开阅读全文