1.如何获得当前文件路径常用:
(1).Test.class.getResource("")得到的是当前类FileTest.class文件的URI目录。不包括自己!
(2).Test.class.getResource("/")得到的是当前的classpath的绝对URI路径。
(3).Thread.currentThread().getContextClassLoader().getResource("")得到的也是当前ClassPath的绝对URI路径。
(4).Test.class.getClassLoader().getResource("")得到的也是当前ClassPath的绝对URI路径。
(5).ClassLoader.getSystemResource("")得到的也是当前ClassPath的绝对URI路径。尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径,后面可以看出得出结果五花八门。
(6) new File("").getAbsolutePath()也可用。
2.Web服务器
(1).Tomcat在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
(2).Resin不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET的路径为根.比如用新建文件法测试File f = new File("a.htm");这个a.htm在resin的安装目录下
(3).如何读文件使用ServletContext.getResourceAsStream()就可以
(4).获得文件真实路径String? file_real_path=ServletContext.getRealPath("mypath/filename"); ?不建议使用request.getRealPath("/");
3.文件操作的类,不建议使用,可以使用commons io类
此处省略三千行代码。
4.遗留问题目前new FileInputStream()只会使用绝对路径,相对没用过,因为要相对于web服务器地址,比较麻烦还不如写个配置文件来的快哪5.按Java文件类型分类读取配置文件配置文件是应用系统中不可缺少的,可以增加程序的灵活性。
java.util.Properties是从jdk1.2就有的类,一直到现在都支持load ()方法,jdk1.4以后save(output,string) ->store(output,string)。如果只是单纯的读,根本不存在烦恼的问题。
web层可以通过 Thread.currentThread().getContextClassLoader().getResourceAsStream("xx.properties") 获取;
Application可以通过new FileInputStream("xx.properties");直接在classes一级获取。
关键是有时我们需要通过web修改配置文件,我们不能将路径写死了。经过测试觉得有以下心得:
1.servlet中读写。
如果运用Struts 或者Servlet可以直接在初始化参数中配置,调用时根据servletcontext的getRealPath("/")获取真实路径,再根据String file = this.servlet.getInitParameter("abc");获取相对的WEB-INF的相对路径。
例:
- InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.properties");
- Properties prop = new Properties();
- prop.load(input);
- input.close();
- OutputStream out = new FileOutputStream(path);
- prop.setProperty("abc", “test");
- prop.store(out, “CtestC");
- out.close();
2.直接在jsp中操作,通过jsp内置对象获取可操作的绝对地址。
例:// jsp页面
- String path = pageContext.getServletContext().getRealPath("/");
- String realPath = path+"/WEB-INF/classes/abc.properties";
- InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties");
-
-
- prop.load(in);
- in.close();
- OutputStream out = new FileOutputStream(path);
- prop.setProperty("abc", “abcccccc");
- prop.store(out, “CtestC");
- out.close();
3.只通过Java程序操作资源文件
- InputStream in = new FileInputStream("abc.properties");
- OutputStream out = new FileOutputStream("abc.properties");
>>1.web环境下得到系统路径
-
-
-
-
- public void deleteFileInPath(HttpServletRequest request, HttpServletResponse response) {
- ServletContext sc = request.getSession().getServletContext();
- String rootpath = sc.getRealPath("");
- File myPath = new File(rootpath);
- if (myPath.isDirectory()) {
- String strSubs[] = myPath.list();
- for (int i = 0; i < strSubs.length; i++) {
-
- String type = strSubs[i].substring(strSubs[i].indexOf(".")+1);
-
- File file = new File(rootpath + "\\" + strSubs[i]);
- if (file.isFile() && type.equals("bmp")) {
- file.delete();
- }
- }
- }
- }
>>2.非web环境下得到系统路径:
项目排列如图所示:

第二个试题非web环境下获得系统路径:
- package com.autonavi.parsexml;
-
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.DocumentBuilder;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
-
- public class XmlParser {
-
-
- public static void main(String[] args) {
-
- try {
-
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-
-
- DocumentBuilder db = dbf.newDocumentBuilder();
-
- System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
- String url = Thread.currentThread().getContextClassLoader().getResource("").toString();
-
- System.out.println("url: " + url);
-
-
- Document dm = db.parse(url + "config/mjy.xml");
-
-
- NodeList nl = dm.getElementsByTagName("state");
-
- System.out.println("the length=" + nl.getLength());
-
- for (int i = 0; i < nl.getLength(); i++) {
-
-
- Element ele = (Element) nl.item(i);
-
-
-
- NamedNodeMap nnm = ele.getAttributes();
-
- for (int j = 0; j < nnm.getLength(); j++) {
- Node node3 = nnm.item(j);
- System.out.println("名称为:" + node3.getNodeName());
- System.out.println("值为:" + node3.getNodeValue());
-
- }
- }
-
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- }
-
- }
项目排列:

主要是下面这几句代码:
- String url = Thread.currentThread().getContextClassLoader().getResource("").toString();
-
- Document dm = db.parse(url + "config/mjy.xml");
- Thread.currentThread().getContextClassLoader().getResource("");
上面这句代码表示:当前ClassPath的绝对URI路径
|