Studying java8 (such as reading a file using Stream)

Paste only the source with your own memo.

import java.io.BufferedWriter;
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.stream.Stream;

/**
 *Studying file reading using Stream
 * @author komikcomik
 *
 */
public class FilesLinesTest {

	public static void main(String[] args) {

		FileSystem fs = FileSystems.getDefault();
		Path inputPath = fs.getPath("resouces/test.txt");

		/*Read file*/
		try (Stream<String> stream = Files.lines(inputPath, StandardCharsets.UTF_8)) {
			stream.forEach(System.out::println);
		} catch (IOException e) {
			//TODO auto-generated catch block
			e.printStackTrace();
		}

		/*Writing a file*/
		Path outputPath = fs.getPath("resouces/output.txt");
		try (BufferedWriter bw = Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
			bw.write("Ahhhh\n");
			bw.write("Good\n");
			bw.write("Uuuuuuu\n");
		} catch (IOException e) {
			//TODO auto-generated catch block
			e.printStackTrace();
		}
	}
}

Recommended Posts

Studying java8 (such as reading a file using Stream)
Upload a file using Java HttpURLConnection
Studying Java 8 (Stream)
Reading a file using Java's Scanner class
[Personal memo] Reading a file using BufferedReader
Java11: Run Java code in a single file as is
[Java] Create a temporary file
Make a rhombus using Java
Run a batch file from Java
Create a Java project using Eclipse
I tried using Java8 Stream API
[Java] Generate a narrowed list from multiple lists using the Stream API
Data processing using stream API from Java 8
Try using the Stream API in Java
Sort by multiple conditions using Java Stream
What is the best file reading (Java)
Why does Java call a file a class?
[Personal memo] Writing a file using BufferedWriter
Build by specifying docker as a file
Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
[Java] Creating an Excel file using Apache POI
Read a string in a PDF file with Java
Write to a file using ShiftJIS-Read a file (Kotlin / JVM)
A story about trying to operate JAVA File
A bat file that uses Java in windows
To manually deploy Struts2 as a war file
Java8 Stream duplicate check (write neatly using Collector)
[Java] How to operate List using Stream API
Ssh connect using SSHJ from a Java 6 app