读取Resource相对路径文件内容(新方式)
文件地址
代码
package com.demo.task;
import com.demo.factory.WorldFilter;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Component
public class ScheduleTest {
@Autowired
private ThreadPoolExecutor myThreadPool;
@Scheduled(fixedRate = 5000)
public void test1() throws IOException {
Resource resource = new ClassPathResource("111.json");
InputStream stream = resource.getInputStream();
String toString = IOUtils.toString(stream, Charset.defaultCharset());
System.out.println(toString);
}
}
源码分析:
本文来自博客园,作者:土木转行的人才,转载请注明原文链接