read file in classpath

前端 未结 3 1845
长情又很酷
长情又很酷 2020-12-07 18:12

Here is what I want to do and I am wondering if there is any Spring classes that will help with implementing. I don\'t have to use spring for this particular problem, I\'m

3条回答
  •  不思量自难忘°
    2020-12-07 18:47

    Change . to / as the path separator and use getResourceAsStream:

    reader = new BufferedReader(new InputStreamReader(
        getClass().getClassLoader().getResourceAsStream(
            "com/company/app/dao/sql/SqlQueryFile.sql")));
    

    or

    reader = new BufferedReader(new InputStreamReader(
        getClass().getResourceAsStream(
            "/com/company/app/dao/sql/SqlQueryFile.sql")));
    

    Note the leading slash when using Class.getResourceAsStream() vs ClassLoader.getResourceAsStream. getSystemResourceAsStream uses the system classloader which isn't what you want.

    I suspect that using slashes instead of dots would work for ClassPathResource too.

提交回复
热议问题