程序代码很简单:
package com.mzy.domain;
import org.junit.jupiter.api.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.FileCopyUtils;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
/**
-
Created by miaozhiyan on 2017/7/24.
*/
public class Test3 {
public static void main(String[] args) {
try {
Resource resource1 = new ClassPathResource("img/tex1.txt");
InputStream is1 = resource1.getInputStream();Resource resource2 = new ClassPathResource("img/tex2.txt"); InputStream is2 = resource2.getInputStream(); System.out.println("res1:"+resource1.getFilename()); System.out.println("res2:"+resource2.getFilename()); EncodedResource encRes1 = new EncodedResource(resource1,"UTF-8"); String content1 = FileCopyUtils.copyToString(encRes1.getReader()); System.out.println("content1:"+content1); FileCopyUtils.copy(resource1.getFile(),new File(resource2.getFile().getParent()+"tex..txt")); byte [] b1 = FileCopyUtils.copyToByteArray(is1); FileCopyUtils.copy(b1,resource2.getFile()); EncodedResource encRes2 = new EncodedResource(resource2,"UTF-8"); String content2 = FileCopyUtils.copyToString(new FileReader(resource2.getFile())); System.out.println("content2:"+content2); }catch (Exception e){ }
}
}
可是为什么输出都输出出来了,打开文件,里面还是空的?tex2是空的,tex3也没有?这是为什么啊?