当前位置:首页 » 编程语言 » java文件类路径

java文件类路径

发布时间: 2023-03-24 09:00:32

java里文件路径怎么写

File
file
=
new
File("D:\\123.txt");
你这种不用绝对路径是不行的,
只有一个方法,在web工程启动servlet中获取到webroot路径,在servlet的init中使用String
webRoot
=
getServletContext().getRealPath("/");获取,然后使用这webRoot变量追加路径,再new
File(),这样的话要求就是,你的服务必须要启动,否则不会init,无法得到工程发布目录的相对路径

㈡ java项目中文件的路径

java项目中文件的路径-方法大全

一、 相对路径的获得

说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)

System.getProperty("user.dir");

上述相对路径中,java项目中的文件是相对于项目的根目录web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于tomcat安装目录in)

二 类加载目录的获得(即当运行时某一类时获得其装载目录)
1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)

InputStreamis=TestAction.class.getClassLoader().getResourceAsStream("test.txt");(test.txt文件的路径为 项目名src est.txt;类TestPath所在包的第一级目录位于src目录下)

三 web项目根目录的获得(发布之后)
1 从servlet出发

可建立一个servlet在其的init方法中写入如下语句(没有请求的话会抛空指针导常)

ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (关键)
结果形如:F: omcat-6.0.36webapps est(test为项目名字)

如果是调用了s1.getRealPath("")则输出F: omcat-6.0.36webapps est(少了一个"")

2 从httpServletRequest出发(没有请求的话会抛空指针导常)

String path=request.getSession().getServletContext().getRealPath("/");

结果形如:F: omcat-6.0.36webapps est

四 classpath的获取(在Eclipse中为获得src或者classes目录的路径),放在监听器,可以窗口启动获取路径

方法一Thread.currentThread().getContextClassLoader().getResource("").getPath()

String path = Thread.currentThread().getContextClassLoader()

.getResource("").getPath();

System.out.println("path========"+ path);输出:path========/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/


方法二JdomParse.class.getClassLoader().getResource("").getPath()(JdomParse为src某一个包中的类,下同)

eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);

输出:JdomParse.class.getClassLoader().getResource-/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/

另外,如果想把文件放在某一包中,则可以 通过以下方式获得到文件(先定位到该包的最后一级目录)

eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);

输出:JdomParse.class.getResource--/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/

(JdomParse为src目录下jdom包中的类)

四 属性文件的读取:

方法 一

InputStream in = lnewBufferedInputStream(newFileInputStream(name));

Properties p =newProperties();p.load(in);

注意路径的问题,做执行之后就可以调用p.getProperty("name")得到对应属性的值

方法二

Locale locale =Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest",locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);

工程src目录下propertiesTest.properties(名字后缀必须为properties)文件内容如下:

test=hello word

不通过Servlet获取路径

第一种实现

Java代码

URL url = ClassLoader.getSystemClassLoader().getResource("./");

File file =newFile(url.getPath());

File parentFile =newFile(file.getParent());

System.out.println("webRoot:"+parentFile.getParent());

第二种实现
首先写一个接听类 (推荐使用,容器启动时就执行,不会抛空指针异常,适合做定时器任务来删除服务器文件的路径)

Java代码:

package com.chinacreator.report.listener;

import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

/**

* @authorxiaoqun.yi

*/

public class PathListener {

private staticServletContext servletContext;

public voidcontextDestroyed(ServletContextEvent sce) {

this.servletContext= sce.getServletContext();

System.out.println("path=======:"+servletContext.getRealPath("/"));

}

public voidcontextInitialized(ServletContextEvent arg0) {

}

}


在web.xml中加入如下配置

Java代码 :

<listener>

<listener-class>com.chinacreator.report.listener.PathListener</listener-class>

</listener>

五、Java中的getResourceAsStream有以下几种:
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由 ClassLoader(类加载器)(获取资源)

2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。

3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,当然这和具体的容器实现有关。

4. Jsp下的application内置对象就是上面的ServletContext的一种实现。

其次,getResourceAsStream 用法大致有以下几种:

第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class ,同时有资源文件myfile.xml

那么,应该有如下代码:

me.class.getResourceAsStream("myfile.xml");

第二:在me.class目录的子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml

那么,应该有如下代码:

me.class.getResourceAsStream("file/myfile.xml");

第三:不在me.class目录下,也不在子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml

那么,应该有如下代码:

me.class.getResourceAsStream("/com/x/file/myfile.xml");

总结一下,可能只是两种写法

第一:前面有 “ / ”

“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject

me.class.getResourceAsStream("/com/x/file/myfile.xml");

第二:前面没有 “ / ”

代表当前类的目录

me.class.getResourceAsStream("myfile.xml");

me.class.getResourceAsStream("file/myfile.xml");

㈢ java如何获取类的绝对路径

1 用servlet获取

1.1 获取项目的绝对路径

request.getSession().getServletContext().getRealPath("")

1.2 获取浏览器地址

request.getRequestURL()

1.3 获取当前文件的绝对路径

request.getSession().getServletContext().getRealPath(request.getRequestURI())
2.获取当前的classpath路径

String a2=类名.class.getResource("").toString();
String a3=DBConnection.class.getResource("/").toString();
String a4=DBConnection.class.getClassLoader().getResource("").toString();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
//输出很好理解

3、获取文件的绝对路径
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"项目名\\WebContent\\文件";

㈣ JAVA中如何得到文件路径

java文件中获得路径
Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.class文件)所在路径
ClassLoader.getSystemResource("")
Class_Name.class.getClassLoader().getResource("")
Class_Name.class .getResource("/")
Class_Name.class .getResource("") // 获得当前类所在路径
System.getProperty("user.dir") // 获得项目根目录的绝对路径
System.getProperty("java.class.path") //得到类路径和包路径
打印输出依次如下:
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F:\work_litao\uri_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

㈤ 在java中怎么获得,本文件的路径

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以
通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:

publicclassPathTest
{
publicstaticvoidmain(String[]args)
{
Filefile=newFile(".\src\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}

getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的
路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。
getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。

下面是上面程序在我电脑上的输出:

G:xhuojkonw.src
G:xhuojkonwsrc

热点内容
压缩性胸闷 发布:2024-05-08 08:18:18 浏览:526
电脑哔哩哔哩pc端的缓存 发布:2024-05-08 08:17:05 浏览:169
王者服务器崩溃估计什么时候好 发布:2024-05-08 08:16:51 浏览:574
qj压缩机 发布:2024-05-08 08:10:13 浏览:841
dhcp服务器可以分配什么参数 发布:2024-05-08 08:07:36 浏览:958
跨象限编程 发布:2024-05-08 07:58:37 浏览:61
java多对一 发布:2024-05-08 07:58:33 浏览:641
苹果怎么创建文件夹 发布:2024-05-08 07:53:34 浏览:917
html连接sql数据库 发布:2024-05-08 07:53:28 浏览:736
网易云盘无法上传 发布:2024-05-08 07:48:42 浏览:598