package com.smart.resource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class FileSourceExample { public static void main(String[] args) {
try {
String filePath = "E:\\learn\\spring4.x\\chapter4\\src\\main\\resources\\conf\\test.txt"; //使用系统文件路径方式加载文件
WritableResource res1 = new PathResource(filePath); //使用类路径方式加载文件
Resource res2 = new ClassPathResource("conf/test.txt"); //使用WritableResource接口写资源文件
OutputStream stream1 = res1.getOutputStream();
stream1.write("欢迎光临\n小春论坛".getBytes());
stream1.close(); //使用Resource接口读资源文件
InputStream ins1 = res1.getInputStream();
InputStream ins2 = res2.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = ins1.read()) != -1) {
baos.write(i);
}
System.out.println(baos.toString()); System.out.println("res1: " + res1.getFilename());
System.out.println("res2: " + res2.getFilename()); } catch (IOException e) {
e.printStackTrace();
}
}
}

《精通Spring4.X企业应用开发实战》读后感第四章(资源访问)的更多相关文章

  1. 《精通Spring4.x企业应用开发实战》第三章

    这一章节主要介绍SpringBoot的使用,也是学习的重点内容,之后就打算用SpringBoot来写后台,所以提前看一下还是很有必要的. 3.SpringBoot概况 3.1.1SpringBoot发 ...

  2. 《精通Spring4.X企业应用开发实战》读后感第七章(创建增强类)

  3. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP基础知识、jdk动态代理,CGLib动态代理)

  4. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP概念)

  5. 《精通Spring4.X企业应用开发实战》读后感第六章(容器事件)

  6. 《精通Spring4.X企业应用开发实战》读后感第六章(国际化)

  7. 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)

  8. 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)

  9. 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)

随机推荐

  1. Tomcat学习笔记【1】--- WEB服务器、JavaEE、Tomcat背景、Tomcat版本

    本文主要讲学习Tomcat需要知道的基础知识. 一 Web服务器 1.1 简介 Web服务器可以解析HTTP协议.当Web服务器接收到一个HTTP请求,会返回一个HTTP响应,例如送回一个HTML页面 ...

  2. 题解 P3805 【【模板】manacher算法】

    题解 P3805 [[模板]manacher算法] 我们先看两个字符串: ABCCBA ABCDCBA 显然这两字符串是回文的 然而两个串的对称中心的特性不同,第一个串,它的对称中心在两个C中间,然而 ...

  3. UVa 11586 - Train Tracks

    题目:给你一些积木碎片,每一个碎片的两端仅仅能是凸或凹(M或F).凸凹可拼起来.是否能拼成一个环. 分析:图论.欧拉回路.推断入度等于出度就可以,即M和F同样且大于1组. 说明:╮(╯▽╰)╭. #i ...

  4. ABap-小技巧

    if FIELD cn '0123456789'. *&如果字符串包含‘数字’    STOP.  endif. 同理到字母‘ABCDEFG*’ 'abcdefg*' '/' '\' 等其它字 ...

  5. Spring项目中使用jackson序列化key为对象Map

    1.注入ObjectMapper2.注册类HistoricTaskInstance的序列化和反序列化类HistoricTaskInstanceKeySerializer,HistoricTaskIns ...

  6. 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器

    参考: 1,使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器笔记(一)http://blog.csdn.net/xdwyyan/article/details/4319 ...

  7. Java for LeetCode 120 Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. Java for LeetCode 086

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. PAT 乙级 1081. 检查密码 (15) 【字符串】

    题目链接 https://www.patest.cn/contests/pat-b-practise/1081 思路 有一个坑点 可能会输入空格 也就是说 要用 geline 或者 gets() 然后 ...

  10. EntityFramework codefirst

    一.Entity Framework 迁移命令(get-help EntityFramework) Enable-Migrations 启用迁移 Add-Migration 为挂起的Model变化添加 ...