Java Stream: File Operation
In this section, we see how to utilize Java stream in file I/O operation.
1. File Read Operation
Let’s understand file read operation through the given example
// Java Program to demonstrate
// File Read Operation
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class GFG {
// Method to filter strings of a given length and
// convert them to uppercase
private static List<String>
filterAndConvertToUpper(Stream<String> stream,
int length)
{
return stream.filter(s -> s.length() == length)
.map(String::toUpperCase)
.collect(Collectors.toList());
}
public static void main(String[] args)
{
// Replace with the
// actual file path
String fileName = "path/to/your/file.txt";
// Step 1: Create a Stream of lines from the
// file
try (Stream<String> lines
= Files.lines(Paths.get(fileName))) {
List<String> filteredStrings
= filterAndConvertToUpper(lines, 5);
System.out.println(
"Filtered strings with length 5 (converted to uppercase): "
+ filteredStrings);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Input:
Geeks
gfg
geeks
w3wiki
Coder
Guys
Output:
Filtered strings with length 5 (converted to uppercase): [GEEKS, GEEKS, CODER]
2. File Write Operation
Let’s understand file write operation through the given example
// Java Program to demonstrate
// File Write Operation
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
String[] words
= { "Geeks", "for", "Geeks", "Hello", "World" };
// Replace with the
// actual file path
String fileName = "path/to/your/file.txt";
// Step 1: Create a PrintWriter to write to the
// file
try (PrintWriter pw
= new PrintWriter(Files.newBufferedWriter(
Paths.get(fileName)))) {
// Step 2: Use Stream to write each word to the
// file
Stream.of(words).forEach(pw::println);
// Step 3: Print success message to the console
System.out.println(
"Words written to the file successfully.");
}
catch (IOException e) {
// Step 4: Handle any IO exception that occurs
// during the file writing process
e.printStackTrace();
}
}
}
Output:
Words written to the file successfully.
Java 8 Stream Tutorial
Java 8 introduces Stream, which is a new abstract layer, and some new additional packages in Java 8 called java.util.stream. A Stream is a sequence of components that can be processed sequentially. These packages include classes, interfaces, and enum to allow functional-style operations on the elements.
The stream can be used by importing java.util.stream package. Stream API is used to process collections of objects. Streams are designed to be efficient and can support improving your program’s performance by allowing you to avoid unnecessary loops and iterations. Streams can be used for filtering, collecting, printing, and converting from one data structure to another, etc.
This Java 8 Stream Tutorial will cover all the basic to advanced concepts of Java 8 stream like Java 8 filter and collect operations, and real-life examples of Java 8 streams.