[TOC]java
URL url = this.getClass ().getResource ("/a.txt"); Resources.asCharSource (url, StandardCharsets.UTF_8) .readLines () forEach (System.out::println); String file = ResourceUtils.getFile ("classpath:a.txt").getPath (); // 經過Spring工具類 String file = this.getClass ().getResource ("/a.txt").getFile ().substring (1); // 經過JDK java.nio.file.Files.readAllLines (Path.of (file)).forEach (System.out::println); // 最佳方式 File file = ResourceUtils.getFile ("classpath:a.txt"); com.google.common.io.Files.readLines (file, Charsets.UTF_8).forEach (System.out::println);
/** * 獲取兩個日期間隔的全部日期 * * @param startDate 開始日期 * @param endDate 結束日期 * @return 全部日期的集合 */ static List<LocalDate> queryWeek(LocalDate startDate, LocalDate endDate, String week) { String englishWeek = weekFormat (week); long days = ChronoUnit.DAYS.between (startDate, endDate); if (days < 0) { throw new DateTimeException ("開始時間不能晚於結束時間!"); } return Stream.iterate (startDate, date -> date.plusDays (1)) .limit (days + 1) // 總的天數是間隔天數+1 .filter (date -> Objects.equals (date.getDayOfWeek ().toString (), englishWeek)) .collect (Collectors.toList ()); }