Home > AI > Language > Java >

read local json to string (method 1)

Example: read resources/asq-1.json

public static void main(String[] args) {
    SpringApplication.run(PteApplication.class, args);
    try {
        testResourceFile("asq-1.json"); 
    }catch (IOException e) {
        e.printStackTrace();
    }
}

public static void testResourceFile(String fileName) throws IOException {
    File resource = new ClassPathResource(fileName).getFile();
    String text = new String(Files.readAllBytes(resource.toPath()));
    System.out.println(text);
}

Leave a Reply