Full example code and resources

This commit is contained in:
susetech
2020-09-19 16:39:55 +05:30
parent 00b00e6e77
commit 13888f806e
6 changed files with 188 additions and 27 deletions

View File

@@ -1,8 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fileswithstreams</groupId>
<artifactId>fileswithstreams</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fileswithstreams</name>
<description>File management with streams</description>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fileswithstreams</groupId>
<artifactId>fileswithstreams</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fileswithstreams</name>
<description>File management with streams</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,18 @@
package io.reflectoring.fileswithstreams;
public class Cake {
private int id;
private String name;
private int price;
public Cake(int id, String name, int price) {
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Cake [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}

View File

@@ -1,30 +1,100 @@
package io.reflectoring.fileswithstreams;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FilesWithStreams {
public static final String fileName = "src/main/resources/paths.properties";
static String folderPath = "src/main/resources/books";
static String filePath = "src/main/resources/books/bookIndex.txt";
static String jarFile = "src/main/resources/books/books.zip";
static String csvPath = "src/main/resources/cakes.csv";
static String utfFilePath = "src/main/resources/input.txt";
static String jarFile = "src/main/resources/books.zip";
public static void main(String[] args) throws IOException {
// processWithStream();
// readLineByLineUsingFiles();
System.out.println("-------------------Files with Streams------------------ ");
System.out.println("-------------------Introductory Example------------------ ");
processWithStream();
System.out.println(
"\r\n" + "-------------------Example 1 - Reading line by line from a file------------------ " + "\r\n");
readLineByLineUsingFiles();
System.out.println(
"\r\n" + "-------------------Example 2 - Reading with Buffered Reader------------------" + "\r\n");
readLineByLineUsingBufferedReader();
readFilterCountUsingFiles();
// printJarFileContents();
// printMatchingJarEntries();
// readWithParallelStreamAndPrint();
System.out.println(
"\r\n" + "-------------------Example 3 - Reading all lines from a file------------------ " + "\r\n");
readAllLinesUsingFiles();
System.out.println(
"\r\n" + "...................Example 4 - Reading with parallel streams------------------ " + "\r\n");
readWithParallelStreamAndPrint();
System.out.println(
"\r\n" + "-------------------Example 5 - Reading UTF-encoded file------------------ " + "\r\n");
readUtfEncodedFile();
System.out.println(
"\r\n" + "-------------------Example 6 - Reading, Filtering and Counting------------------ " + "\r\n");
readFilterCountFromFile();
System.out.println("\r\n" + "-------------------Example 7 - Splitting Words------------------ " + "\r\n");
splitWordsFromFile();
System.out.println("\r\n" + "-------------------Example 8 - Loading from CSV - ------------------ " + "\r\n");
loadItemsFromCsvFile();
System.out.println("\r\n" + "...................Example 9 - Listing Directories------------------ " + "\r\n");
listDirectories();
System.out
.println("\r\n" + "...................Example 10 - Listing Regular Files------------------ " + "\r\n");
listRegularFiles();
System.out.println(
"\r\n" + "...................Example 11 - Walking Files Recursively------------------ " + "\r\n");
walkFilesRecursively();
System.out.println("\r\n" + "...................Example 12 - Finding Files------------------ " + "\r\n");
findFiles();
System.out.println(
"\r\n" + "...................Example 13 - Printing JAR fie contents------------------ " + "\r\n");
printJarFileContents();
System.out.println("...................Example 14 - Printing Matching JAR entries------------------ " + "\r\n");
printMatchingJarEntries();
}
static void processWithStream() {
@@ -44,15 +114,76 @@ public class FilesWithStreams {
lines.forEach(System.out::println);
}
}
static void readFilterCountUsingFiles() throws IOException {
try (Stream<String> lines = Files.lines(Path.of(filePath))) {
static void readAllLinesUsingFiles() throws IOException {
List<String> strList = Files.readAllLines(Path.of(filePath));
Stream<String> lines = strList.stream();
lines.forEach(System.out::println);
}
static void readUtfEncodedFile() throws IOException {
try (Stream<String> lines = Files.lines(Path.of(utfFilePath), StandardCharsets.UTF_8)) {
lines.forEach(System.out::println);
}
}
static void loadItemsFromCsvFile() throws IOException {
Pattern pattern = Pattern.compile(",");
try (Stream<String> lines = Files.lines(Path.of(csvPath))) {
List<Cake> cakes = lines.skip(1).map(line -> {
String[] arr = pattern.split(line);
return new Cake(Integer.parseInt(arr[0]), arr[1], Integer.parseInt(arr[2]));
}).collect(Collectors.toList());
cakes.forEach(System.out::println);
}
}
static void readFilterCountFromFile() throws IOException {
try (Stream<String> lines = Files.lines(Path.of(filePath))) {
long i = lines.filter(line -> line.startsWith("A")).count();
System.out.println("Count of lines starting with 'A' is " + i);
System.out.println("The count of lines starting with 'A' is " + i);
}
}
static void splitWordsFromFile() throws IOException {
try (Stream<String> lines = Files.lines(Path.of(filePath))) {
Stream<String> words = lines.flatMap(line -> Stream.of(line.split("\\W+")));
Set<String> wordSet = words.collect(Collectors.toSet());
System.out.println(wordSet);
}
}
static void listDirectories() throws IOException {
try (Stream<Path> paths = Files.list(Path.of(folderPath))) {
paths.filter(Files::isDirectory).forEach(System.out::println);
}
}
static void listRegularFiles() throws IOException {
try (Stream<Path> paths = Files.list(Path.of(folderPath))) {
paths.filter(Files::isRegularFile).forEach(System.out::println);
}
}
static void walkFilesRecursively() throws IOException {
try (Stream<Path> stream = Files.walk(Path.of(folderPath))) {
stream.filter(Files::isRegularFile).forEach(System.out::println);
}
}
static void findFiles() throws IOException {
int depth = Integer.MAX_VALUE;
try (Stream<Path> paths = Files.find(Path.of(folderPath), depth, (path, attr) -> {
return attr.isRegularFile() && path.toString().endsWith(".pdf");
})) {
paths.forEach(System.out::println);
}
}
static void printJarFileContents() throws IOException {
try (JarFile jFile = new JarFile(jarFile)) {
jFile.stream().forEach(file -> System.out.println(file));
@@ -70,10 +201,8 @@ public class FilesWithStreams {
}
static void readWithParallelStreamAndPrint() throws IOException {
List<String> lines = Files.readAllLines(Path.of(filePath));
try (Stream<String> stream = lines.parallelStream()) {
stream.forEach(System.out::println);
try (Stream<String> lines = (Files.lines(Path.of(filePath)).parallel())) {
lines.forEach(System.out::println);
}
}

View File

@@ -0,0 +1,6 @@
#Cakes
1, Pound Cake,100
2, Red Velvet Cake,500
3, Carrot Cake,300
4, Sponge Cake,400
5, Chiffon Cake,600
1 #Cakes
2 1, Pound Cake,100
3 2, Red Velvet Cake,500
4 3, Carrot Cake,300
5 4, Sponge Cake,400
6 5, Chiffon Cake,600

View File

@@ -1,4 +1 @@
Master, you are the sole goal of human life
We are all but slaves of wishes
Putting bar to our advancement
You are the one and only true lord who can bring us upto that level
akarui _ あかるい _ bright