09-spring学习-资源访问接口
目標:
1,掌握Resource接口的使用。
2,掌握ResourceLoader接口的使用。
3,掌握各種資源數據的讀取操作。
具體內容:
要想進行資源讀取操作,首先想到IO包中提供的操作類。
但是,有如下問題:
1,這些類的互相操作,難道太高,很多人對IO領悟并不是很徹底。
2,IO支持的讀取有限且復雜。
-讀取jar包里面的文件呢?
-讀取不同資源文件的時候,操作不統一,例如:讀取文件,網絡讀取;
所以在整個spring設計過程中充分考慮了IO操作中的種種操作問題,提供了新的資源訪問處理支持。而整個操作的關鍵在于:Resource接口。這個接口表示所有的可用資源讀取,
而這個接口定義了如下幾個常用方法:
1,取得資源的數據長度:public long contentLength()。
2,判斷資源是否存在:public boolean exists()
3,取得資源對應的文件信息:public File ?getFile();
4,取得資源完整網絡路徑:getUrl()
5,判斷資源是否打開:public boolean ?isOpen()
6,最后一次修改日期:public long lastModifid()
7,創建一個操作的資源:public Resource createRelative()
?
Resource本身是一個接口,要想使用這個接口,需要使用他的子類:
ByteArrayResource (內存讀取),ClassPathResource(ClassPath讀取),FileSystemResource(文件讀取)
?
讀取不同資源
首先按照基本開發進行基本資源的讀取。
1,讀取內存資源:ByteArrayResource
構造方法:public ByteArrayResource(byte [] byteArray);
范例:實現內存讀取:
package com.Resource.Demo; import java.util.Scanner;import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource;public class ByteResource {public static void main(String[] args) throws Exception {//此處的內存處理流與之前IO里面的byteArrayInputStream使用形式類似Resource resource=new ByteArrayResource("helloworld".getBytes());//就取得更多資源信息來說,比InputStream強System.out.println("數據長度"+resource.contentLength());//如果給出的是InputStream,那么可以利用Scannner簡化讀取。//getInputStream是通過InputStreamSource父接口繼承而來的方法Scanner scan=new Scanner(resource.getInputStream());while(scan.hasNext()){System.out.println(scan.next());}} }輸出結果:
數據長度10 helloworld文件讀取:FileSystemResource
構造方法:public FileSystemResource(File file);--直接傳入File
構造方法:public FileSystemResource(String path);--直接寫文件路徑
范例:進行文件讀取:
package com.Resource.Demo; import java.io.File; import java.util.Scanner;import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource;public class FileResource {public static void main(String[] args) throws Exception {//此處的內存處理流與之前IO里面的byteArrayInputStream使用形式類似Resource resource=new FileSystemResource("D:"+File.separator+"test.txt");//就取得更多資源信息來說,比InputStream強System.out.println("數據長度:"+resource.contentLength());//如果給出的是InputStream,那么可以利用Scannner簡化讀取。//getInputStream是通過InputStreamSource父接口繼承而來的方法Scanner scan=new Scanner(resource.getInputStream());//表示/n是換行符,而不是結束符scan.useDelimiter("\n");while(scan.hasNext()){System.out.println(scan.next());}} }?
3,CLASSPATH讀取:CLASSPATHRESOURCE
構造方法:public ClassPathResource(String path):
只要保存在了CLASSPATH環境下的路徑信息都可以通過此類讀取進來。
范例:讀取applicationContext.XML文件
如果要進行文件的讀取,必須要有完整的路徑,也就是說,默認情況下,要想讀取指定的資源,那么必須想辦法拼湊出路徑,
(還需要取得一系列的系統屬性,等一系列操作)。
package com.Resource.Demo; import java.util.Scanner; import org.springframework.core.io.Resource; import org.springframework.core.io.ClassPathResource;;public class ClassResource {public static void main(String[] args) throws Exception {//此處的內存處理流與之前IO里面的byteArrayInputStream使用形式類似Resource resource=new ClassPathResource("applicationContext.xml");//就取得更多資源信息來說,比InputStream強System.out.println("數據長度:"+resource.contentLength());//如果給出的是InputStream,那么可以利用Scannner簡化讀取。//getInputStream是通過InputStreamSource父接口繼承而來的方法Scanner scan=new Scanner(resource.getInputStream());//表示/n是換行符,而不是結束符scan.useDelimiter("\n");while(scan.hasNext()){System.out.println(scan.next());}} }輸出結果:
?
?
ResourceLoader接口
ResourceLoader接口主要作用是進行ResourceLoader接口對象實例化使用的。這個接口的定義如下:
1,讀取指定的資源信息,:public Resource getResource(String location);
2,取得類加載器:public ClassLoader getClassLoader();
ResourceLoader是一個接口,于是要使用這個接口,必須知道它的子類:
DefaultResourceLoader,利用這個子類就可以實現ResourceLoader接口實例化。
但是資源操作的問題并不在于Resource或者ResourceLoader接口,以及其一堆子類,而關鍵在于這個定位的字符串:
文件讀取資源:“file:路徑”;
CLASSPATH讀取:“classpath:路徑”;
網絡讀取:“http://路徑”;
?
范例:進行文件讀取:
package com.Resource.Demo; import java.io.File; import java.util.Scanner; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class FileResourceLoader {public static void main(String[] args) throws Exception {ResourceLoader loader=new DefaultResourceLoader();Resource resource=loader.getResource("file:D:"+File.separator+"test.txt");System.out.println("數據長度:"+resource.contentLength());Scanner scan=new Scanner(resource.getInputStream());scan.useDelimiter("\n");while(scan.hasNext()){System.out.println(scan.next());}} }路徑只寫了一個字符串,就可以讀取了。
?
范例:讀取ClassPath路徑
package com.Resource.Demo; import java.io.File; import java.util.Scanner; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class ClassPathResourceLoader {public static void main(String[] args) throws Exception {ResourceLoader loader=new DefaultResourceLoader();Resource resource=loader.getResource("classpath:applicationContext.xml");System.out.println("數據長度:"+resource.contentLength());Scanner scan=new Scanner(resource.getInputStream());scan.useDelimiter("\n");while(scan.hasNext()){System.out.println(scan.next());}} }范例:讀取網絡資源
在tomcat這個目錄下新建一個note.txt文件。
讀取代碼:
package com.Resource.Demo; import java.util.Scanner; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class HttpResourceLoader {public static void main(String[] args) throws Exception {ResourceLoader loader=new DefaultResourceLoader();Resource resource=loader.getResource("http://localhost/note.txt");System.out.println("數據長度:"+resource.contentLength());Scanner scan=new Scanner(resource.getInputStream());scan.useDelimiter("\n");while(scan.hasNext()){System.out.println(scan.next());}} }?
?
所有的讀取的操作過程之中,可以清楚的看到,都是利用字符串來進行資源定位,
核心的設計思想就是:利用合理的字符串格式,來進行更加復雜的操作。
?
?
轉載于:https://www.cnblogs.com/alsf/p/8053969.html
總結
以上是生活随笔為你收集整理的09-spring学习-资源访问接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 12.16——周总
- 下一篇: 第一次冲刺-个人工作总结01