[Java IO]基礎:讀取檔案

來源:互聯網
上載者:User

標籤:讀取檔案   java   

1. 讀取檔案路徑字串1.1 利用ClassLoader類方法獲得檔案路徑
System.out.println(this.getClass().getClassLoader().getResource(".").getPath());// 得到的是項目的class檔案路徑System.out.println(this.getClass().getClassLoader().getResource("").getPath());// 得到的是項目的class檔案路徑System.out.println(this.getClass().getResource("/").getPath());// 得到的是項目的class檔案路徑System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());System.out.println(Thread.currentThread().getContextClassLoader().getResource(".").getPath());System.out.println(this.getClass().getResource("").getPath());// ★得到當前檔案所在的包路徑System.out.println(this.getClass().getResource(".").getPath());//★得到當前檔案所在的包路徑

結果:
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/com/test/
/D:/workspace/DGCertTest/build/classes/com/test/

1.2 通過getCanonicalPath()和getAbsolutePath()獲得檔案路徑
File directory = new File("");// 設定為當前檔案夾 System.out.println(directory.getCanonicalPath());// 擷取標準的路徑 System.out.println(directory.getAbsolutePath());// 擷取絕對路徑 //結果:D:\workspace\java_baseFile panFu = new File("/");// 設定為當前檔案夾 System.out.println(panFu.getCanonicalPath());//得到所在磁碟System.out.println(directory.getAbsolutePath());// 擷取絕對路徑 //結果:D:\
2. 通過檔案路徑擷取IO流2.1 FileInputStream:讀取本地磁碟特定位置的檔案

String path=”D:\HwTemp\dongguan_test\test.xml”;
FileInputStream fis=new FileInputStream(new File(path));
但是在這種方式在換個環境測試時又要重新拷貝到別人的磁碟上,沒有放到工程中,用工程相對路徑來訪問方便。
這樣工程換個環境能直接運行,即String ->FileInputStream

//工程路徑+檔案所在工程的相對路徑(用)String path = System.getProperty("user.dir")+"\\src\\file.txt";//將所有的上面得到的路徑中"\"轉換成"\\"path = path.replaceAll("\\\\","\\\\\\\\");System.out.println(path);FileInputStream fis = new FileInputStream(new File(path));//D:\\workspace\\java_base\\src\\file.txt

注意:字串裡 \\表示一個”\” 但relaceAll裡的 第一個參數是一個Regex,在正則裡\\\\表示一個”\”
但System.getProperty("user.dir")Java代碼如果放到jsp中,得到的將是IDE的安裝路徑。所以該代碼不能用jsp頁面中

2.2 InputStream:方法get****AsStream讀取本工程中檔案:

該類方法返回InputStream
參考文檔:Java讀取工程裡的檔案

  • 方法一:(未驗證)將xml檔案放在WEB-INF目錄下,然後
InputStream is=getServletContext().getResourceAsStream( "/WEB-INF/xmlfile.xml" );
  • 方法二:將xml檔案放在/WEB-INF/classes目錄下或classpath的jar包中,則可以使用ClassLoader的靜態方法getSystemResourceAsStream(String s)讀取;
String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";InputStream in=ClassLoader.getSystemResourceAsStream(s_xmlpath);
  • 方法三:xml在隨意某個包路徑下
String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";ClassLoader classLoader=HotspotXmlParser.class.getClassLoader();InputStream in=classLoader.getResourceAsStream(s_xmlpath);
3. 附件3.1 附件1:通過System.getProperties()擷取系統參數
Properties props=System.getProperties(); //系統屬性System.out.println("Java的運行環境版本:"+props.getProperty("java.version"));System.out.println("Java的運行環境供應商:"+props.getProperty("java.vendor"));System.out.println("Java供應商的URL:"+props.getProperty("java.vendor.url"));System.out.println("Java的安裝路徑:"+props.getProperty("java.home"));System.out.println("Java的虛擬機器規範版本:"+props.getProperty("java.vm.specification.version"));System.out.println("Java的虛擬機器規範供應商:"+props.getProperty("java.vm.specification.vendor"));System.out.println("Java的虛擬機器正式名稱:"+props.getProperty("java.vm.specification.name"));System.out.println("Java的虛擬機器實現版本:"+props.getProperty("java.vm.version"));System.out.println("Java的虛擬機器實現供應商:"+props.getProperty("java.vm.vendor"));System.out.println("Java的虛擬機器實現名稱:"+props.getProperty("java.vm.name"));System.out.println("Java運行時環境規範版本:"+props.getProperty("java.specification.version"));System.out.println("Java運行時環境規範供應商:"+props.getProperty("java.specification.vender"));System.out.println("Java運行時環境正式名稱:"+props.getProperty("java.specification.name"));System.out.println("Java的類格式版本號碼:"+props.getProperty("java.class.version"));System.out.println("Java的類路徑:"+props.getProperty("java.class.path"));System.out.println("載入庫時搜尋的路徑列表:"+props.getProperty("java.library.path"));System.out.println("預設的臨時檔案路徑:"+props.getProperty("java.io.tmpdir"));System.out.println("一個或多個擴充目錄的路徑:"+props.getProperty("java.ext.dirs"));System.out.println("作業系統的名稱:"+props.getProperty("os.name"));System.out.println("作業系統的構架:"+props.getProperty("os.arch"));System.out.println("作業系統的版本:"+props.getProperty("os.version"));System.out.println("檔案分隔字元:"+props.getProperty("file.separator"));   //在 unix 系統中是"/"System.out.println("路徑分隔字元:"+props.getProperty("path.separator"));   //在 unix 系統中是":"System.out.println("行分隔字元:"+props.getProperty("line.separator"));   //在 unix 系統中是"/n"System.out.println("使用者的賬戶名稱:"+props.getProperty("user.name"));System.out.println("使用者的主目錄:"+props.getProperty("user.home"));System.out.println("使用者的當前工作目錄:"+props.getProperty("user.dir"));
3.2 附件2:最基本的路徑轉換
@Testpublic void test_getXmlPath() {    String path = Thread.currentThread().getContextClassLoader().getResource("").toString();    path = path.replace(‘/‘, ‘\\‘); // 將/換成\    path = path.replace("file:", ""); // 去掉file:    path = path.replace("classes\\", ""); // 去掉class\    path = path.substring(1); // 去掉第一個\,如 \D:\JavaWeb...    path += "web.xml";    System.out.println(path);}
@Testpublic void test_getXmlPath() {    String path = System.getProperty("user.dir")+"\\src\\file.txt";    System.out.println();    //★將所有的上面得到的路徑中"\"轉換成"\\"    path = path.replaceAll("\\\\","\\\\\\\\");    System.out.println(path);}

[Java IO]基礎:讀取檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.