1. 程式人生 > >File file = new File("路徑名") 路徑名的2種寫法

File file = new File("路徑名") 路徑名的2種寫法

效果 log .cn 絕對路徑 fileinput ima 現在 註意 file

項目的結構:相同顏色是同級的

技術分享


技術分享技術分享


    public static void getValue(String key){ //傳入"time"
        Properties prop = new Properties();
        Properties prop2 = new Properties();
        Properties prop3 = new Properties();
        File file = new File("D:\\java\\content\\eclipse-win64\\S\\java\\fd.properties");
        File file2 
= new File("java\\fd.properties"); File file3 = new File("fd2.properties"); try { //裝載配置文件 prop.load(new FileInputStream(file)); prop2.load(new FileInputStream(file2)); prop3.load(new FileInputStream(file3)); } catch (IOException e) { e.printStackTrace(); }
//返回獲取的值 System.out.println(prop.getProperty(key) + prop2.getProperty(key)+prop3.getProperty(key)); }
8 8 9




fd.properties的內容
技術分享

技術分享

項目名是 S

技術分享 技術分享

點開bin文件夾

技術分享

註意這個fd.properties文件

發現:

只有在src或者java文件夾下的java文件或資源文件才會編譯,然後通過打包,會復制到commlib中


後面有2個ok

	/*
	1.絕對路徑

    a.帶盤符,如E:/book.xml

    b.以http開頭,http://img.baidu.com/img/book.jpg

2.相對路徑

  a.帶"/"開頭,如/book.xml

  b.不帶"/"開頭,如book.xml




現在項目結構如下,在ParseXML類中操作,我實驗項目System.getProperty("user.dir")=E:\ProjectTest\javaEE

絕對路徑:

new
 File("E:/ProjectTest/javaEE/src/com/ly/javaee/xml/dom4j/book.xml")---OK
相對路徑 new File("book.xml")----報錯(此時相當於System.getProperty("user.dir")+"book.xml",這是文件book.xml不存在user.dir下面) new File("src/com/ly/javaee/xml/dom4j/book.xml")----OK(不帶"/" 可見在項目中相對的是以項目名為根路徑,此時相當於System.getProperty("user.dir")+"src/com/ly/javaee/xml/dom4j/book.xml") new File("/book.xml")----以下可以看出本JVM運行在E盤下,此時帶"/"就相當於"E:/book.xml",JVM運行在哪個盤符就以哪個為根路徑 new File("./book.xml");---報錯(同new File("book.xml")效果)

  

File file = new File("路徑名") 路徑名的2種寫法