
在最常用的读取properties文件的方式--->“通过javalangClass类,
getResourceAsStream(String name) 方法来实现”;
代码:InputStream in = getClass()getResourceAsStream("资源Name")
Java是由Sun微系统公司所发展出来的程序语言,它本身是一种对象导向(Object-Oriented)的程序语言。JAVA目前在手机上应用最多的就是JAVA游戏。
第一种:
File f = new File(thisgetClass()getResource("/")getPath());
Systemoutprintln(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
获取当前类的所在工程路径;
如果不加“/”
File f = new File(thisgetClass()getResource("")getPath());
Systemoutprintln(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;
第二种:
File directory = new File("");//参数为空
String courseFile = directorygetCanonicalPath() ;
Systemoutprintln(courseFile);
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;
第三种:
URL xmlpath = thisgetClass()getClassLoader()getResource("selectedtxt");
Systemoutprintln(xmlpath);
结果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selectedtxt
获取当前工程src目录下selectedtxt文件的路径
第四种:
Systemoutprintln(SystemgetProperty("userdir"));
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径
第五种:
Systemoutprintln( SystemgetProperty("javaclasspath"));
结果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
获取当前工程路径
packagecomfutureutil;importjavaioFileNotFoundException;importjavaioIOException;importjavaioInputStream;importjavautilProperties;/读取资源配置文件@authorcoder/@SuppressWarnings("serial")publicclassCommonPropertiesUtil{@SuppressWarnings("static-access")publicstaticStringgetContextPath(){StringcontextPath="";try{//加载src目录下的文件的几种方式//注意当使用getClass()方式而不是getClassLoader()时资源文件前的"/"不能省略//InputStreamstream=thisgetClass()getResourceAsStream("/commonproperties");//InputStreamstream=ThreadcurrentThread()getClass()getResourceAsStream("/commonproperties");InputStreamstream=ThreadcurrentThread()getContextClassLoader()getResourceAsStream("commonproperties");//谨记这种方式是错误的//InputStreamstream=ThreadcurrentThread()getClass()getClassLoader()getResourceAsStream("commonproperties");//InputStreamstream=ThreadcurrentThread()getClass()getClassLoader()getSystemResourceAsStream("commonproperties");//InputStreamstream=thisgetClass()getClassLoader()getSystemResourceAsStream("commonproperties");//InputStreamstream=thisgetClass()getClassLoader()getResourceAsStream("commonproperties");//InputStreamstream=thisgetClass()getClassLoader()getSystemResourceAsStream("commonproperties");//InputStreamstream=CommonPropertiesUtilclassgetClassLoader()getResourceAsStream("commonproperties");//InputStreamstream=CommonPropertiesUtilclassgetClass()getClassLoader()getSystemClassLoader()getResourceAsStream("commonproperties");//InputStreamstream=CommonPropertiesUtilclassgetClass()getClassLoader()getSystemClassLoader()getSystemResourceAsStream("commonproperties");
是读取src目录下的资源文件不咯?这样吧,程序里面直接 取到啊,propertiesget(key)
private static Properties properties = new Properties();
static{
try {
propertiesload(SiteUrlclassgetClassLoader()getResourceAsStream("siteurlproperties"));
} catch (IOException e) {
eprintStackTrace();
}
}
这里面我把se跟ee方面获取路径的给你列举出来了,希望对你有用
Java中使用的路径,分为两种:绝对路径和相对路径。归根结底,Java本质上只能使用绝对路径来寻找资源。所有的相对路径寻找资源的方法,都不过是一些便利方法。不过是API在底层帮助我们构建了绝对路径,从而找到资源的!
在开发Web方面的应用时, 经常需要获取服务器中当前WebRoot的物理路径。
如果是Servlet , Action , Controller, 或者Filter , Listener , 拦截器等相关类时, 我们只需要获得ServletContext, 然后通过ServletContextgetRealPath("/")来获取当前应用在服务器上的物理地址。
如果在类中取不到ServletContext时,有两种方式可以做到:
1)利用Java的类加载机制:调用 XXXclassgetClassLoader()getResource(""); 方法来获取到ClassPath , 然后处理获得WebRoot目录。
这种方式只能是该class在WebRoot/WEB-INF/classes下才能生效, 如果该class被打包到一个jar文件中, 则该方法失效。这时就应该用下面一种方式。
2)spring框架的思路,在WEB-INF/webxml中,创建一个webAppRootKey的param,指定一个值(默认为webapproot)作为键值,然后通过Listener, 或者Filter,或者Servlet 执行String webAppRootKey = getServletContext()getRealPath("/"); 并将webAppRootKey对应的webapproot 分别作为Key,Value写到System Properties系统属性中。之后在程序中通过SystemgetProperty("webapproot")来获得WebRoot的物理路径。
根据第二种的思路,我们还可以再扩展一下。不过对于在部署在一台服务器中的应用来说,若还不是你所需请再往下看。
下面是一些得到classpath和当前类的绝对路径的一些方法。你可使用其中的一些方法来得到你需要的资源的绝对路径:
1DebitNoteActionclassgetResource("")
得到的是当前类FileTestclass文件的URI目录。不包括自己!
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
atacarnet/src/com/evi/modules/atacarnet/action/
2DebitNoteActionclassgetResource("/")
得到的是当前的classpath的绝对URI路径。
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
3ThreadcurrentThread()getContextClassLoader()getResource("")
得到的也是当前ClassPath的绝对URI路径
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
推荐使用该方法获取。
4DebitNoteActionclassgetClassLoader()getResource("") 或ClassLoadergetSystemResource("")
得到的也是当前ClassPath的绝对URI路径。
如:file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/
5取得服务器相对路径
SystemgetProperty("userdir")
例如:E:\apache-tomcat-5516\apache-tomcat-5516\bin
6取得项目中的绝对路径
一般用requestgetRealPath("/")或requestgetRealPath("/config/")
但现在不提倡使用requestgetRealPath("/")了,大家可试用ServletContextgetRealPath("/")方法得到Web应用程序的根目录的绝对路径。
要取得src的文件非常容易,因为src是默认的相对目录,比如你说要取得src下com目录的testjava文件,你只需要这样就够了
File f = new File(com/testjava);
但如果我要取得不在src目录或者WebRoot目录下的文件呢,而是要从src或者WebRoot同级的目录中取呢,比如说doc吧。
我的硬方法是这样实现的:
String path = thisgetServletContext()getRealPath("/");
Properties p = new Properties();
pload(new FileInputStream(new File(pathsubstring(0,(pathlastIndexOf("\\WebRoot") + 1)) + "doc/dbproperties")));
Systemoutprintln(pgetProperty("driverName"));
-------------------------------
另:Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
假定你的web application 名称为news,你在浏览器中输入请求路径:>
以上就是关于JAVA中如何读取src下所有的properties文件全部的内容,包括:JAVA中如何读取src下所有的properties文件、java中获取工程中res目录路径的方法、Java加载src目录下文件的几种方式,谈谈你的见解等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)