标签:div context demo color resource efault 文件 throw text
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("\n") ; 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("\n") ; 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("\n") ; 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("\n") ; 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("\n") ; 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("\n") ; 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(); }
标签:div context demo color resource efault 文件 throw text
原文地址:https://www.cnblogs.com/blog-747674599/p/spring.html