获取当前项目的路径:
ClassLoaderTest.class.getClassLoader().getResourceAsStream("log4j.properties");
// 代码示例
package com.etongdai.depository.ctr.base;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class ClassLoaderTest {
public static void main(String args[]){
ClassLoader test =Thread.currentThread().getContextClassLoader();
System.out.println("parent.."+test.getParent());
System.out.println("parent..parent"+test.getParent().getParent());
String src =Class.class.getResource("/").getFile();
String pp=src=System.getProperty("user.dir");
URL yy =Thread.currentThread().getContextClassLoader().getResource("");
//InputStream aa =Class.class.getResourceAsStream("log4j.properties");
InputStream aa =ClassLoaderTest.class.getClassLoader().getResourceAsStream("log4j.properties");
try {
//getClassLoader().getResourceAsStream("beans.xml")
// InputStream aa =new FileInputStream("D:/wpl/www.txt");
byte[] bb =new byte[1024];
int len =0;
int temp =0;
while((temp =aa.read())!=-1){
bb[len]=(byte)temp;
len++;
}
aa.close();
System.out.println(new String(bb,0,len)+"....test.."+yy.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}