jdk7及之后读取

       String queryPath = "classpath:sql/user.sql";
       try {
           Path path = Paths.get(ResourceUtils.getFile(queryPath).getAbsolutePath());
           String result = String.join(" ", Files.readAllLines(path));
           System.out.println(result);
       }catch (Exception e) {
           System.out.println(e.getMessage());
       }

传统读取

        try {
            String queryPath = "classpath:sql/user.sql";
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(ResourceUtils.getFile(queryPath))));
            StringBuffer buffer = new StringBuffer();
            String line = reader.readLine();
            while(line != null) {
                buffer.append(line).append(" ");
                line = reader.readLine();
            }
            reader.close();
            System.out.println(buffer.toString());
        } catch (Exception e) {

        }

测试

        try {
            String queryPath = "classpath:sql/user.sql";
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(ResourceUtils.getFile(queryPath))));
            StringBuffer buffer = new StringBuffer();
            String line = reader.readLine();
            while(line != null) {
                buffer.append(line).append(" ");
                line = reader.readLine();
            }
            reader.close();

            Path path = Paths.get(ResourceUtils.getFile(queryPath).getAbsolutePath());
            String sql = String.join(" ", Files.readAllLines(path));

            Assert.assertEquals(buffer.toString().trim(), sql.trim());

        } catch (Exception e) {

        }
posted on 2021-02-25 08:58  漫夭  阅读(34)  评论(0编辑  收藏  举报