1 16 package com.jdon.util; 17 18 import java.io.InputStream ; 19 import java.net.URL ; 20 import java.io.File ; 21 22 27 public class FileLocator { 28 29 35 public String getConfPathXmlFile(String filePathName){ 36 int i = filePathName.lastIndexOf(".xml"); 37 String name = filePathName.substring(0, i); 38 name = name.replace('.', '/'); 39 name += ".xml"; 40 return getConfFile(name); 41 } 42 43 48 public InputStream getConfPathXmlStream(String filePathName){ 49 int i = filePathName.lastIndexOf(".xml"); 50 String name = filePathName.substring(0, i); 51 name = name.replace('.', '/'); 52 name += ".xml"; 53 return getConfStream(name); 54 } 55 56 57 public String getConfFile(String fileName) { 58 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 59 if (classLoader == null) { 60 classLoader = getClass().getClassLoader(); 61 } 62 URL confURL = classLoader.getResource(fileName); 63 if (confURL == null) 64 confURL = classLoader.getResource("META-INF/" + fileName); 65 if (confURL == null) { 66 return null; 67 } else { 68 File file1 = new File (confURL.getFile()); 69 if (file1.isFile()) { 70 System.out.println(" locate file: " + confURL.getFile()); 71 return confURL.getFile(); 72 } else { 73 System.err.println(" it is not a file: " + confURL.getFile()); 74 return null; 75 } 76 } 77 } 78 79 public InputStream getConfStream(String fileName) { 80 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 81 if (classLoader == null) { 82 classLoader = this.getClass().getClassLoader(); 83 } 84 InputStream stream = classLoader.getResourceAsStream(fileName); 85 if (stream == null) 86 stream = classLoader.getResourceAsStream("META-INF/" + fileName); 87 88 return stream; 89 } 90 91 } 92 | Popular Tags |