标签:new tde resource 长度 nio code ted port source
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 * @throws IOException 20 */ 21 @Test 22 public void getResouce() throws IOException{ 23 Resource resource =new ClassPathResource("beanFactoryTest.xml"); 24 InputStream in = resource.getInputStream(); 25 String fileName = resource.getFilename(); 26 String description = resource.getDescription(); 27 long contentLength = resource.contentLength(); 28 File file = resource.getFile(); 29 30 31 System.out.println("文件名:"+fileName); 32 System.out.println("描述:"+description); 33 System.out.println("正文长度:"+contentLength); 34 35 36 /** 37 * 有File对象就可以读取到正文的数据-----方法1 38 */ 39 List<String> list = Files.readAllLines(file.toPath()); 40 for (String string : list) { 41 System.out.println(string); 42 } 43 44 45 System.out.println("------------------------------------------------------------------------------------------------- "); 46 /** 47 * 有File对象可以读取正文的数据 ----方法2 48 */ 49 BufferedReader br = new BufferedReader(new FileReader(file)); 50 String str = null; 51 while((str=br.readLine()) != null){ 52 System.out.println(str); 53 } 54 55 System.out.println("--------------------------------------------------------------------------------------------------"); 56 /** 57 * 有InputStream可以读取正文数据 58 */ 59 int contentLen = in.available(); 60 byte[] st = new byte[contentLen]; 61 in.read(st); 62 System.out.println(new String(st)); 63 64 } 65 }
【Spring】获取资源文件+从File+从InputStream对象获取正文数据
标签:new tde resource 长度 nio code ted port source
原文地址:http://www.cnblogs.com/sxdcgaq8080/p/6184311.html