
requestgetScheme()得到协议如:>public class PathTest
{
public static void main(String[] args)
{
File file = new File("\\src\\baidu");
Systemoutprintln(filegetAbsolutePath());
try
{
Systemoutprintln(filegetCanonicalPath());
} catch (IOException e)
{
eprintStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\\src\baidu
G:\xhuoj\konw\src\baidu
java获取根路径有两种方式:
1),在servlet可以用一下方法取得:
requestgetRealPath(“/”) 例如:filepach = requestgetRealPath(“/”) ”//upload//”;
2),不从jsp,或servlet中获取,只从普通java类中获取:
String path =
getClass()getProtectionDomain()getCodeSource()getLocation()getPath();
SAXReader() saxReader = new SAXReader();
if(pathindexOf(“WEB-INF”)>0){
path = pathsubstring(0,pathindexOf(“/WEB-INF/classes”) 16);
// ‘/WEB-INF/classes’为16位
document = saxReaderread(path filename);
}else{
document = saxReaderread(getClass()getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass()getProtectionDomain()getCodeSource()getLocation()getPath();
<!--EndFragment-->
我记得当初我用了个new File在网络上传输文件,结果失败了,文件地址全是new 在 服务器上的地址,你可以试试用new File,然后得到他的toURI,然后再toURL,不知道你的问题合适不,你可以试试
读取配置文件 , 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();
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
String titlePath = null;
String recPath = null;
String templatePath = null;
String replacePath = null;
SecHandAnalyse yse = new SecHandAnalyse();
titlePath = "/conf/sechand/naalee/titleconf";
recPath = "F:\\naaleerec";
templatePath= "/conf/sechand/naalee/templateconf";
replacePath= "/conf/sechand/naalee/replaceconf";
有办法能使它只需要通过一个路径就找到其它3个的。CONF文件吗?`````
解析:
可以的 采用 File 类 和 FileFilter 接口:
public static void main(String args[]){
File directory = new File("/conf/sechand/naalee/");
Systemoutprintln(directoryisDirectory());
File[] fileList = directorylistFiles(new javaioFileFilter() {
public boolean accept(File file) {
if(filegetName()endsWith("conf")) return true;
else return false;
}});
for (File file : fileList) {
Systemoutprintln(filegetPath());
}
}
以上就是关于在一个Java WEB项目里然后获取项目工程的路径 不要Tomcat下的路径全部的内容,包括:在一个Java WEB项目里然后获取项目工程的路径 不要Tomcat下的路径、在java项目中如何获取某个文件的路径、java如何获得linux下web路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)