spring-Resource-01
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-Resource-01相关的知识,希望对你有一定的参考价值。
1、读取不同资源
字节码
public class ByteArrayResourceDemo { public static void main(String[] args) throws IOException { // 设置了内存的输入资源 Resource resource = new ByteArrayResource("Hello World".getBytes()) ; if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter(" ") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } } }
从文件读取
public static void main(String[] args) throws IOException { File file = new File("D:" + File.separator + "msg.txt") ; // 设置了内存的输入资源 Resource resource = new FileSystemResource(file) ; if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter(" ") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
classpath读取
public static void main(String[] args) throws IOException { // 设置了内存的输入资源 Resource resource = new ClassPathResource("applicationContext.xml") ; if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter(" ") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
DefaultResourceLoader
http 通过网络读取
file通过文件读取
classpath通过classpath读取
public static void main(String[] args) throws IOException { ResourceLoader loader = new DefaultResourceLoader() ; Resource resource = loader.getResource("file:d:\msg.txt"); if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter(" ") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
public static void main(String[] args) throws IOException { ResourceLoader loader = new DefaultResourceLoader() ; Resource resource = loader.getResource("classpath:applicationContext.xml"); if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter(" ") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
Resource自动装配
->MyResource
public class MyResource { private Resource resource ; public Resource getResource() { return resource; } public void setResource(Resource resource) { this.resource = resource; } public void print() throws IOException { if (this.resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(this.resource.getInputStream()); scan.useDelimiter(" ") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } } }
->applicationContext.xml
<bean id="myres" class="cn.mldn.util.MyResource">
<property name="resource" value="file:d:\msg.txt" />
</bean>
->test
public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); MyResource mr = ctx.getBean("myres",MyResource.class); mr.print(); }
以上是关于spring-Resource-01的主要内容,如果未能解决你的问题,请参考以下文章