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
  • 相关阅读:
    转:asp.net mvc下的多语言方案 包含Html,Javascript和图片
    转:在ASP.NET MVC中通过URL路由实现对多语言的支持
    转:C# lock用法
    转:SQL 关于apply的两种形式cross apply 和 outer apply
    在ASP.NET MVC 中获取当前URL、controller、action
    转:Newtonsoft.Json高级用法
    转:jQuery插件开发全解析
    转:ASP.NET MVC 多语言实现技巧 最简、最易维护和最快速开发
    转:前端js、jQuery实现日期格式化、字符串格式化
    转:.Net内存泄露原因及解决办法
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/6184311.html
Copyright © 2011-2022 走看看