读取Resource相对路径文件内容(新方式)

文件地址

image

代码

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);
    }
}

源码分析:

image

posted @ 2023-01-31 22:07  土木转行的人才  阅读(102)  评论(0编辑  收藏  举报