Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAR中的Spring引导类路径资源_Java_Spring_Spring Boot - Fatal编程技术网

Java JAR中的Spring引导类路径资源

Java JAR中的Spring引导类路径资源,java,spring,spring-boot,Java,Spring,Spring Boot,在我的Spring Boot 1.5应用程序中,我使用ClassPathResource读取应用程序JAR中的静态文件: // ... import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @Slf4j @Service public class MyService { private Resource resource = new Class

在我的Spring Boot 1.5应用程序中,我使用
ClassPathResource
读取应用程序JAR中的静态文件:

// ...
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

@Slf4j
@Service
public class MyService {
    private Resource resource = new ClassPathResource("a.txt");
    private List<String> myStrings;
    public MyService() {
        myStrings = load(resource);
    }

    private List<String> load(Resource resource) {
        try(Stream<String> stream = Files.lines(Paths.get(resource.getURI()))) {
            myStrings = stream.filter(/* my filter */)
                      .collect(Collectors.toList());
        } catch (IOException x) {
            log.error("Failed to read '{}'.", resource.getFilename());
        }
    }
}

如何读取嵌入在我的应用程序JAR中的
ClassPathResource

JDK的
路径。get
无法解析JAR文件中的资源,因此请替换:

    Files.lines(Paths.get(resource.getURI()))
与:

    Files.lines(Paths.get(resource.getURI()))
    new BufferedReader(new InputStreamReader(resource.getInputStream())).lines();