gpt4 book ai didi

Java 8 : Reading a file into a String

转载 作者:行者123 更新时间:2023-11-29 04:07:54 27 4
gpt4 key购买 nike

我在 Controller 的同一个包中有一个json文件,我尝试读取该文件并将其转换为String

new String(Files.readAllBytes(Paths.get("CustomerOrganization.json")));

但是我得到一个错误:

java.nio.file.NoSuchFileException: CustomerOrganization.json

enter image description here

最佳答案

主要使用与您相同的方法:

new String(Files.readAllBytes(Paths.get(CustomerControllerIT.class.getResource("CustomerOrganization.json").toURI())));

但是,如果您需要它在 JAR 中工作,则需要改为这样做:

InputStream inputStream = CustomerControllerIT.class.getResourceAsStream("CustomerOrganization.json");
// TODO pick one of the solutions from below url
// to read the inputStream into a string:
// https://stackoverflow.com/a/35446009/1356047

关于Java 8 : Reading a file into a String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57177831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com