zoukankan      html  css  js  c++  java
  • 【Spring】获取资源文件+从File+从InputStream对象获取正文数据

    1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取
    2.仅有File对象即可获取正文数据
    3.仅有InputStream即可获取正文数据
     1 package com.sxd.test.test1;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.nio.file.Files;
     9 import java.util.List;
    10 
    11 import org.junit.Test;
    12 import org.springframework.core.io.ClassPathResource;
    13 import org.springframework.core.io.Resource;
    14 
    15 public class GetResource {
    16     
    17     /**
    18      * 获取资源文件的 方法之一-----使用Spring架包中的Resource类实现
    19      * 当然 获取资源文件还有不同来源的资源文件有相应的Resource实现:
    20      * FileSystemResource-----文件资源
    21      * ClassPathResource-----ClassPath资源
    22      * UrlResource-----------URL资源
    23      * InputStreamResource---InputStream资源
    24      * ByteArrayResource-----Byte数组资源
    25      * @throws IOException
    26      */
    27     @Test
    28     public void getResouce() throws IOException{
    29         Resource resource =new  ClassPathResource("beanFactoryTest.xml");
    30         InputStream in = resource.getInputStream();
    31         String fileName = resource.getFilename();
    32         String description = resource.getDescription();
    33         long contentLength = resource.contentLength();
    34         File file = resource.getFile();
    35         
    36         
    37         System.out.println("文件名:"+fileName);
    38         System.out.println("描述:"+description);
    39         System.out.println("正文长度:"+contentLength);
    40         
    41         
    42         /**
    43          * 有File对象就可以读取到正文的数据-----方法1
    44          */
    45         List<String> list = Files.readAllLines(file.toPath());
    46         for (String string : list) {
    47             System.out.println(string);
    48         }
    49         
    50         
    51         System.out.println("------------------------------------------------------------------------------------------------- ");
    52         /**
    53          * 有File对象可以读取正文的数据  ----方法2
    54          */
    55         BufferedReader br = new BufferedReader(new FileReader(file));
    56         String str = null;
    57         while((str=br.readLine()) != null){
    58             System.out.println(str);
    59         }
    60         
    61         System.out.println("--------------------------------------------------------------------------------------------------");
    62         /**
    63          * 有InputStream可以读取正文数据
    64          */
    65         int contentLen = in.available();
    66         byte[] st = new byte[contentLen];
    67          in.read(st);
    68          System.out.println(new String(st));
    69         
    70     }
    71 }
    View Code
  • 相关阅读:
    文件路径选择中的三态逻辑
    .net版本号
    使用MSBuild编译vs多个解决方案
    CEF截图
    使用SharpZIpLib写的压缩解压操作类
    软件试用期设置
    list转datatable
    excel 导入
    网站登录简单验证码
    UEditor编辑器
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/6184311.html
Copyright © 2011-2022 走看看