San*_*Rey 4 java file-io file java-8
我在控制器的同一个包中有一个 json 文件,我尝试读取该文件并将其转换为 String
new String(Files.readAllBytes(Paths.get("CustomerOrganization.json")));
Run Code Online (Sandbox Code Playgroud)
但我得到了一个错误:
java.nio.file.NoSuchFileException: CustomerOrganization.json
Run Code Online (Sandbox Code Playgroud)
Jon*_*lin 10
使用与您使用的方法大致相同的方法:
new String(Files.readAllBytes(Paths.get(CustomerControllerIT.class.getResource("CustomerOrganization.json").toURI())));
Run Code Online (Sandbox Code Playgroud)
但是,如果您需要它在 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
Run Code Online (Sandbox Code Playgroud)