
读取配置文件 , xxxproperties放在webroot/WEB-INF/classes/目录下
首先将配置文件转换成InputStream,有两种方式,原理一样,都是通过类加载器得到资源:
(1)InputStream inputStream = ThreadcurrentThread()getContextClassLoader()getResourceAsStream("xxproperties");
(2) InputStream inputStream =
thisgetClass() getClassLoader()getResourceAsStream( "xxproperties" );
调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,
而后在类类型上调用 getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父 子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就 是保证是和类类型同一个加载器加载的。
最后调用了类加载器的getResourceAsStream()方法来加载资源。
(3) 然后加载配置文件,读取属性值
Properties prop = new Properties();
propload(input);
String value = propgetProperty("PropertyName");
inputclose();
import javaioFile;
public class FileDemo {
public static void main(String[] args) {
File file = new File("C:\\Users\\lenovo\\Desktop\\userpng");
Systemoutprintln(filegetAbsolutePath());
StringBuilder sb = new StringBuilder();
File temp = file;
while (tempgetParentFile() != null && tempgetParentFile()getName()length() != 0) {
sbinsert(0, "/" + tempgetParentFile()getName());
temp = tempgetParentFile();
}
sbappend("/");
Systemoutprintln(sb);
}
}
输出
C:\Users\lenovo\Desktop\userpng/Users/lenovo/Desktop/
filegetParent()表示取得父路径
如果不用Fileseparator 还可以先判断 *** 作系统,然后进行字符串 *** 作
Properties props=SystemgetProperties(); //获得系统属性集String osName = propsgetProperty("osname"); // *** 作系统名称
if(osNametoLowerCase()contains("windows")){
//windows 的字符串 *** 作
} else if(){
//其他 *** 作系统的字符串 *** 作
}
getClass()getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)
例如 项目在/D:/workspace/MainStream/Test
在javaProject中,getClass()getResource("/")getFile()toString() 返回:/D:/workspace/MainStream/Test/bin/
public String getCurrentPath(){//取得根目录路径
String rootPath=getClass()getResource("/")getFile()toString();
//当前目录路径
String currentPath1=getClass()getResource("")getFile()toString();
String currentPath2=getClass()getResource("")getFile()toString();
//当前目录的上级目录路径
String parentPath=getClass()getResource("/")getFile()toString();
return rootPath;
}
参考资料:
首先定义一个文件类 File file=new File("d:"+Fileseperator+"demotxt");(这是你的文件的路径)然后取得上层路径就是 filegetParent 得到的就是你截取后的字符串目录
就拿一个项目来说吧。
你项目名称是test,项目下面有test1这个目录,如图
假如在indexjsp文件中引用utiljs文件
你可以用src="js/utiljs或者src="/test1/js/utiljs 这两种方式引用
不加/就是相对路径代表以indexjsp文件的目录(test1)为开始路径
加/代表绝对路径以项目根目录为开始路径(/test)
如果有多处匹配,并输出位置就用Matcher
import javautilregex;
public class FileTest{
public static void main(String[] args){
Matcher m=Patterncompile("/")matcher("my name is /,/said:please find me /");
int count=1;
while(mfind()){
Systemoutprintf("找到第%d个,起始:%d,结束:%d\n",count,mstart(),mend());
count++;
}
}
}
这个是反斜杠的\
import javautilregex;
public class FileTest{
public static void main(String[] args){
Matcher m=Patterncompile("\\\\")matcher("my name is \\,\\said:please find me \\");
int count=1;
while(mfind()){
Systemoutprintf("找到第%d个,起始:%d,结束:%d\n",count,mstart(),mend());
count++;
}
}
}
给一个代码你就知道了,代码里面文件的路径改成你电脑上的文件的路径
import javaioBufferedReader;import javaioFileInputStream;
import javaioIOException;
import javaioInputStreamReader;
/
file IO流读取并输出文件
@author Administrator
/
public class FileIO {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("src/day03/BrDemojava");// 要读的文件路径
InputStreamReader isr = new InputStreamReader(fis);// 字符流
BufferedReader br = new BufferedReader(isr); // 缓冲
String line = null;
while ((line = brreadLine()) != null) {// 字符不等于空
Systemoutprintln(line);// 一行一行地输出
}
brclose();// 关闭文件
}
}
以上就是关于java web工程,读取配置文件路径问题全部的内容,包括:java web工程,读取配置文件路径问题、java怎么去除路径最后文件名,获取文件夹路径、通过java获取当前项目路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)