< prev index next >

src/java.base/share/classes/java/util/Scanner.java

Print this page




2880      *
2881      * <p>After stream pipeline execution completes, this scanner is left in an indeterminate
2882      * state and cannot be reused.
2883      *
2884      * <p>If this scanner contains a resource that must be released, this scanner
2885      * should be closed, either by calling its {@link #close} method, or by
2886      * closing the returned stream. Closing the stream will close the underlying scanner.
2887      * {@code IllegalStateException} is thrown if the scanner has been closed when this
2888      * method is called, or if this scanner is closed during stream pipeline execution.
2889      *
2890      * <p>As with the {@link #findWithinHorizon findWithinHorizon()} methods, this method
2891      * might block waiting for additional input, and it might buffer an unbounded amount of
2892      * input searching for a match.
2893      *
2894      * @apiNote
2895      * For example, the following code will read a file and return a list
2896      * of all sequences of characters consisting of seven or more Latin capital
2897      * letters:
2898      *
2899      * <pre>{@code
2900      * try (Scanner sc = new Scanner(Paths.get("input.txt"))) {
2901      *     Pattern pat = Pattern.compile("[A-Z]{7,}");
2902      *     List<String> capWords = sc.findAll(pat)
2903      *                               .map(MatchResult::group)
2904      *                               .collect(Collectors.toList());
2905      * }
2906      * }</pre>
2907      *
2908      * @param pattern the pattern to be matched
2909      * @return a sequential stream of match results
2910      * @throws NullPointerException if pattern is null
2911      * @throws IllegalStateException if this scanner is closed
2912      * @since 9
2913      */
2914     public Stream<MatchResult> findAll(Pattern pattern) {
2915         Objects.requireNonNull(pattern);
2916         ensureOpen();
2917         Stream<MatchResult> stream = StreamSupport.stream(new FindSpliterator(pattern), false);
2918         return stream.onClose(this::close);
2919     }
2920 




2880      *
2881      * <p>After stream pipeline execution completes, this scanner is left in an indeterminate
2882      * state and cannot be reused.
2883      *
2884      * <p>If this scanner contains a resource that must be released, this scanner
2885      * should be closed, either by calling its {@link #close} method, or by
2886      * closing the returned stream. Closing the stream will close the underlying scanner.
2887      * {@code IllegalStateException} is thrown if the scanner has been closed when this
2888      * method is called, or if this scanner is closed during stream pipeline execution.
2889      *
2890      * <p>As with the {@link #findWithinHorizon findWithinHorizon()} methods, this method
2891      * might block waiting for additional input, and it might buffer an unbounded amount of
2892      * input searching for a match.
2893      *
2894      * @apiNote
2895      * For example, the following code will read a file and return a list
2896      * of all sequences of characters consisting of seven or more Latin capital
2897      * letters:
2898      *
2899      * <pre>{@code
2900      * try (Scanner sc = new Scanner(Path.get("input.txt"))) {
2901      *     Pattern pat = Pattern.compile("[A-Z]{7,}");
2902      *     List<String> capWords = sc.findAll(pat)
2903      *                               .map(MatchResult::group)
2904      *                               .collect(Collectors.toList());
2905      * }
2906      * }</pre>
2907      *
2908      * @param pattern the pattern to be matched
2909      * @return a sequential stream of match results
2910      * @throws NullPointerException if pattern is null
2911      * @throws IllegalStateException if this scanner is closed
2912      * @since 9
2913      */
2914     public Stream<MatchResult> findAll(Pattern pattern) {
2915         Objects.requireNonNull(pattern);
2916         ensureOpen();
2917         Stream<MatchResult> stream = StreamSupport.stream(new FindSpliterator(pattern), false);
2918         return stream.onClose(this::close);
2919     }
2920 


< prev index next >