利用spring提供的工具类读取资源文件

原文:https://blog.csdn.net/loveschen/article/details/54021242







利用spring提供的工具类读取资源文件

很多情况下自己写读取资源文件,都是通过I/O相关的类,File,InputStream,OutPutStream这些,很多方法可能都不记得,要去查资料,很不方便。相比I/O,spring的 org.springframework.core.io.Resource接口抽象层面更高,更适合操作资源文件。Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名、URL 地址以及资源内容的操作方法。
spring提供的访问资源文件的方式:

  • FileSystemResource
    通过 FileSystemResource 以文件系统绝对路径的方式进行访问
public void file(){
    String filePath = "D:/file/test.properties";//绝对路径
    Resource resource1 = new FileSystemResource(filePath);
    InputStream is = file.getInputStream();//获取inputstream
    File file = file.getFile(); //获取file对象
    ....
}
  • ClassPathResource
    通过 ClassPathResource 以类路径的方式进行访问
public void file(){
    String filePath = "config/test.properties";//类路径,编译后classes目录下
    Resource resource1 = new ClassPathResource(filePath);
    InputStream is = file.getInputStream();//获取inputstream
    File file = file.getFile(); //获取file对象
    ....
}
  • 通过ServletContextResource 以相对于 Web 应用根目录的方式进行访问

除了用这些Resource 实现类,spring还提供了ResourceUtils 工具类,它支持“classpath:”和“file:”的地址前缀,它能够从指定的地址加载文件资源

public void file(){
    File file1 = ResourceUtils.getFile("classpath:config/test.properties"); //类路径
    String path = "file:D:/file/test.properties"; //绝对路径
    File file2 = ResourceUtils.getFile(path);     
}

本地化文件资源(摘自IBM)

本地化文件资源是一组通过本地化标识名进行特殊命名的文件,Spring 提供的 LocalizedResourceHelper 允许通过文件资源基名和本地化实体获取匹配的本地化文件资源并以 Resource 对象返回。假设在类路径的 i18n 目录下,拥有一组基名为 message 的本地化文件资源,我们通过以下实例演示获取对应中国大陆和美国的本地化文件资源:

public void file(){
     LocalizedResourceHelper lrHalper = new LocalizedResourceHelper(); 
        // ① 获取对应美国的本地化文件资源
        Resource msg_us = lrHalper.findLocalizedResource("i18n/message", ".properties", 
        Locale.US); 
        // ② 获取对应中国大陆的本地化文件资源
        Resource msg_cn = lrHalper.findLocalizedResource("i18n/message", ".properties", 
        Locale.CHINA); 
        System.out.println("fileName(us):"+msg_us.getFilename()); 
        System.out.println("fileName(cn):"+msg_cn.getFilename());    
}

FileCopyUtils文件内容拷贝

它提供了许多一步式的静态操作方法,能够将文件内容拷贝到一个目标 byte[]、String 甚至一个输出流或输出文件中。下面的实例展示了 FileCopyUtils 具体使用方法:

 public class FileCopyTest { 
    public static void main(String[] args) throws Throwable { 
        Resource res = new ClassPathResource("config/test.properties"); 
        // ① 将文件内容拷贝到一个 byte[] 中
        byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile()); 
        // ② 将文件内容拷贝到一个 StringString fileStr = FileCopyUtils.copyToString(new FileReader(res.getFile())); 
        // ③ 将文件内容拷贝到另一个目标文件
        FileCopyUtils.copy(res.getFile(), 
        new File(res.getFile().getParent()+ "/file2.txt")); 

        // ④ 将文件内容拷贝到一个输出流中
        OutputStream os = new ByteArrayOutputStream(); 
        FileCopyUtils.copy(res.getInputStream(), os); 
    } 
 }
方法 说明
static void copy(byte[] in, File out) 将 byte[] 拷贝到一个文件中
static void copy(byte[] in, OutputStream out) 将 byte[] 拷贝到一个输出流中
static int copy(File in, File out) 将文件拷贝到另一个文件中
static int copy(InputStream in, OutputStream out) 将输入流拷贝到输出流中
static int copy(Reader in, Writer out) 将 Reader 读取的内容拷贝到 Writer 指向目标输出中
static void copy(String in, Writer out) 将字符串拷贝到一个 Writer 指向的目标中

FileCopyUtils 本身和 Resource 没有任何关系,您完全可以在基于 JDK I/O API 的程序中使用这个工具类。

PropertiesLoaderUtils属性文件操作

我们可以通过 java.util.Properties类的load(InputStream inStream) 方法从输入流中加载资源文件,这需要先获取输入流,输入流有涉及到文件,步骤很多,比较繁琐。而Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源,比load方法更具实用性:

public static void main(String[] args) throws Throwable {    
        // ① config/jdbc.properties 是位于类路径下的文件
        Properties props = PropertiesLoaderUtils.loadAllProperties("config/jdbc.properties");
     System.out.println(props.getProperty("jdbc.url")); 
    } 

PropertiesLoaderUtils还有一些很实用的方法,比如
static Properties loadProperties(Resource resource)—从 Resource 中加载属性,static void fillProperties(Properties props, Resource resource)—将 Resource 中的属性数据添加到一个已经存在的 Properties 对象中

资源编码问题

采用Resource实现类加载资源文件时,默认是采用系统编码加载,如果资源文件是采用其他编码方式编码(utf-8),这时候就需要通过 EncodedResource 指定编码格式

public static void main(String[] args) throws Throwable  { 
            Resource res = new ClassPathResource("conf/file1.txt"); 
            // ① 指定文件资源对应的编码格式(UTF-8)
            EncodedResource encRes = new EncodedResource(res,"UTF-8"); 
            // ② 这样才能正确读取文件的内容,而不会出现乱码
            String content  = FileCopyUtils.copyToString(encRes.getReader()); 
            System.out.println(content);  
    }


猜你喜欢

转载自blog.csdn.net/quincylk/article/details/79792779