1 24 25 package com.rift.coad.lib.common; 27 28 import com.rift.coad.lib.common.*; 30 import java.lang.ClassLoader ; 31 import java.io.*; 32 import java.net.URL ; 33 34 35 41 public class ResourceReader { 42 43 private String path = null; 45 private String document = null; 46 47 52 public ResourceReader(String path) throws Exception { 53 this.path = path; 54 loadDocument(getClass().getClassLoader()); 55 } 56 57 58 63 public ResourceReader(String path,ClassLoader classLoader) 64 throws Exception { 65 this.path = path; 66 loadDocument(classLoader); 67 } 68 69 70 76 public String getPath() { 77 return path; 78 } 79 80 81 87 public String getDocument() { 88 return document; 89 } 90 91 92 97 private void loadDocument(ClassLoader classLoader) throws Exception { 98 try { 99 InputStreamReader reader = new InputStreamReader(classLoader. 101 getResourceAsStream(path)); 102 BufferedReader buffReader = new BufferedReader(reader); 103 StringBuffer stringBuffer = new StringBuffer (); 104 char[] buffer = new char[1024]; 105 int length = 0; 106 while (-1 != (length = buffReader.read(buffer))) { 107 stringBuffer.append(buffer,0,length); 108 } 109 reader.close(); 110 document = stringBuffer.toString(); 111 } catch (Exception ex){ 112 throw new Exception ( 113 "Failed to load the file [" + path + "]",ex); 114 } 115 } 116 } 117 | Popular Tags |