JAVA资格认证(关于Java文件路径问题)

JAVA资格认证(关于Java文件路径问题),第1张

JAVA资格认证(关于Java文件路径问题),第2张

1.如何获取当前文件路径
常用:
String类型:system . getproperty(" user . dir ");
综合:
package com . zcjl . test . base;
导入Java . io . file;
公共类测试{
公共静态void main(String[] args)引发异常{
system . out . println(
thread . current thread()。getContextClassLoader()。get resource(" ");
system . out . println(test . class . get class loader()。get resource(" ");
system . out . println(class loader . get system resource(" ");
system . out . println(test . class . get resource(" ");
system . out . println(test . class . get resource("/");
System.out.println(新文件(" ")。getAbsolutePath());
system . out . println(system . getproperty(" user . dir "));
}
}
file:/E:/workSpace/Java Test/target/classes/
file:/E:/workSpace/Java Test/target/classes/
file:/E:/workSpace/Java Test/target/classes/
file:/E:/workSpace/Java Test/target/classes/Java API/
file:/E:/workSpace/Java Test/target/classes/
在web服务中
(1)。weblogic
Web应用程序的系统文件根目录是weblogic安装所在的根目录。
例如:如果您的WebLogic安装在C: \ BEA \ WebLogic 700 …
,则您的文件的根路径是C: \。所以,你访问你的服务器端文件有两种方式:a .使用绝对路径:比如把你的参数文件放在C:\ your config \ your conf . properties,直接使用newfile inputstream("/your config/your conf . properties ");b .使用相对路径:相对路径的根目录是你的webapplication的根路径,也就是WEB-INF的上一级目录。把你的参数文件放在your web app \ your config \ your conf . properties中,这样使用:new file input stream(" your config/your conf . properties ");无论哪种方式,选择。
(2)。Tomcat
到输出系统。类中的GetProperty(" user . dir ");显示的内容是% Tomcat _ home%/bin
(3)。Resin
不是你的JSP放置的相对路径,而是JSP引擎执行这个JSP并编译成SERVLET的路径才是根。比如用new file方法测试file f = new file(" a . htm ");该a.htm位于resin
(4)的安装目录中。相对路径怎么读?getResource或getresourcestream都可以在Java文件中使用
示例:getclass()。getresourcestream(file path);//filePath可以是“/filename”,这里/表示web发布的根路径下的web-INF/classes
。GetClass()。GetClassloader()。GetResourceStream(file path)//file path不是带“/”的
(5)。获取文件的真实路径
string file _ real _ path = request . get real path(" my path/filename ");
通常使用request . get realpath("/");
3。Legacy
目前new FileInputStream()只使用绝对路径,相对于
InputStream In1 = newfile InputStream(" ABC 1 . properties ");//相对路径inputstream in2 = newfile inputstream("/ABC 2 . properties ");//绝对路径,E盘下InputStream in3 = newfile InputStream(" E://ABC 3 . properties ");//相对路径
4。根据Java文件类型读取配置文件
配置文件在应用系统中是必不可少的,可以增加程序的灵活性。java.util.Properties是一个从jdk1.2开始就存在的类,一直到现在都支持load()方法。jdk1.4之后,保存(输出,字符串)->存储(输出,字符串)。如果只是单纯的读书,一点麻烦都没有。web层可以通过thread.currentthread()获取。GetContextClassloader()。
GetResourceStream(" xx . properties ");
可以通过新的文件输入流(" xx.properties ")访问应用程序;直接在类级别获取它。关键是有时候我们需要通过web修改配置文件,不能写死路径。经过测试,感觉有以下体验:
1。在Servlet中读写。如果使用Struts或servlet,可以直接在初始化参数中配置。调用时根据servlet的getRealPath("/")获取真实路径,然后根据字符串File = this . servlet . getint parameter(" ABC ")获取真实路径;获取相对WEB-INF的相对路径。
示例:InputStream input = thread . current thread()。GetContextClassloader()。
GetResourceStream(" ABC . properties ");Properties prop = new Properties();prop.load(输入);input . close();output stream out = new file output stream(path);prop.setProperty("abc "," test ");prop.store(out,"–test-");out . close();
2。直接在jsp中操作,通过jsp内置对象获取可操作的绝对地址。
示例://JSP page string path = page context . getservletcontext()。get realpath("/");string real path = path+"/we b-INF/classes/ABC . properties ";
//java程序inputstream in = getclass()。getclassloader()。getresourcestream(" ABC . properties ");// abc.properties放在webroot/WEB-INF/classes/目录下的prop.load (in)中;in . close();
output stream out = new file output stream(path);// path是通过页面传入的路径prop.setProperty("abc "," abcccc ");prop.store(out,"–test-");out . close();
3。仅通过Java程序操作= newfile inputstream(" ABC . properties ")中的资源文件inputstream//相对路径,项目下的路径
output stream out = new file output stream(" ABC . properties ");

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » JAVA资格认证(关于Java文件路径问题)

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情