I tried using Java8 Stream API

Sample code

I tried to read and write files using StreamApi added in Java8. Please note that I am investigating and writing in about 30 minutes for verification (laugh)


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

 *Stream sample<br />
 *Character code is UTF-Set at 8(Shift_Cannot be referenced in jis)
public class SampleStream {
	/**Read path*/
	private static final String INPUT_PATH = "C:\\temp\\in.txt";
	/**Output path*/
	private static final String OUTPUT_PATH = "C:\\temp\\out.txt";

	public static void main(String[] args) throws Exception {
		//Get FileSystem
		FileSystem fs = FileSystems.getDefault();
		//Set file path
		Path path = fs.getPath(INPUT_PATH);
		//Set the path for output
		Path out = fs.getPath(OUTPUT_PATH);

		//How to read part 1
		// Files.Read using lines
		try (Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8)) {
			stream.filter(s -> s.contains("link")).map(s -> s.replace("html", "form"))
					.map(s -> s.replace("action", "href")).forEach(System.out::println);
		} catch (IOException e) {

		//How to read part 2
		// Files.Read using readAllLines
		List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
		List<String> output = new ArrayList<String>();
		lines.stream().filter(s -> s.contains("link")).forEach(s -> output.add(s));

		//Pack the extracted data in a List and output it as text
		Files.write(out, output, StandardCharsets.UTF_8, StandardOpenOption.APPEND);

		//Output the output data with the extended for statement and check
		for (String put : output) {


It seems that the character code only supports UTF-8, and an exception is thrown when trying to read with Shift_Jis.

I would like to publish the method of reading the directory hierarchy at a later date.

Recommended Posts

I tried using Java8 Stream API
I tried using Elasticsearch API in Java
I tried using Java REPL
Java Stream API
I tried using JWT in Java
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
Data processing using stream API from Java 8
Try using the Stream API in Java
I tried using OpenCV with Java + Tomcat
I tried to summarize the Stream API
I tried using Gson
[Java] Stream API / map
I tried using TestNG
I tried using Galasa
Java8 Stream API practice
[API] I tried using the zip code search API
[Java] How to operate List using Stream API
Java Stream API in 5 minutes
I tried Drools (Java, InputStream)
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
I tried using Apache Wicket
I tried node-jt400 (SQL stream)
[Java] Introduction to Stream API
[Java] Stream API intermediate operation
I tried metaprogramming in Java
I tried Mastodon's Toot and Streaming API in Java
I tried to operate SQS using AWS Java SDK
I tried Java Lambda input / output type ~ Stream version ~
I tried using Log4j2 on a Java EE server
[Java] I tried to implement Yahoo API product search
I tried using an extended for statement in Java
I tried scraping a stock chart using Java (Jsoup)
Export issues using JIRA's Java API
I tried using Spring + Mybatis + DbUnit
I tried to introduce UI animation to Pokedex using Poké API
I tried to interact with Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried to summarize Java learning (1)
I tried using Dapr in Java to facilitate microservice development
[Android] I tried using Coordinator Layout.
I was addicted to using Java's Stream API in Scala
I tried using Pari gp container
I tried using WebAssembly Stadio (2018/4/17 version)
Java 8 ~ Stream API ~ to start now
I tried to summarize Java 8 now
I tried using the CameraX library with Android Java Fragment
I tried to display the calendar on the Eclipse console using Java.
I tried using Realm with Swift UI
I tried using Java's diagnostic tool Arthas
I tried using UICollectionViewListCell added from Xcode12.
I tried Cassandra's Object Mapper for Java
Java9 was included, so I tried jshell.
I tried using Scalar DL with Docker
Sort by multiple conditions using Java Stream
I tried the new era in Java
Try using JSON format API in Java
I tried using OnlineConverter with SpringBoot + JODConverter
Try various Java Stream API methods (now)