程序代码很简单:
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也没有?这是为什么啊?
String content2 = FileCopyUtils.copyToString(new FileReader(resource2.getFile()));
System.out.println("content2:"+content2);
不知道你最后这是干嘛,还用reader,不是应该用printwriter之类的吗
这个给你参考一下:
try {
FileOutputStream os=new FileOutputStream(new File("d:/22.jpg"));//被复制到此文件中
Resource resource=new ClassPathResource("/1.jpg"); //文件源 :找类文件下的这个.jpg文件
FileCopyUtils.copy(resource.getInputStream(),os);
PS:(
//FileOutputStream os=new FileOutputStream(new File(fpt+".jpg"));
//FileSystemResource resource = new FileSystemResource( "E:/union.jpg");
//FileCopyUtils.copy(resource.getInputStream(),os);
FileStstemResource //根据文件路径找
)