-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractAuthorNames.java
47 lines (35 loc) · 1.61 KB
/
ExtractAuthorNames.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.coneva.dic;
import lombok.SneakyThrows;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.concurrent.atomic.AtomicInteger;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
public class ExtractAuthorNames {
static File authors = new File("authors.txt");
public static void main(String[] args) throws IOException {
var profileCounter = new AtomicInteger();
var path = Paths.get("src", "main", "resources", "profiles").toAbsolutePath();
Files.walk(path, FileVisitOption.FOLLOW_LINKS)
.filter(Files::isRegularFile)
.map(ExtractAuthorNames::extractAuthorNameAndBirthday)
.forEach(profile -> writeToFile(profile, profileCounter.incrementAndGet()));
}
@SneakyThrows
private static void writeToFile(String profile, int incrementAndGet) {
var identity = incrementAndGet + " " + profile;
Files.writeString(authors.toPath(), identity + System.lineSeparator(), StandardCharsets.UTF_8, CREATE, APPEND);
}
@SneakyThrows
private static String extractAuthorNameAndBirthday(Path path) {
return Files.readAllLines(path).stream()
.filter(line -> line.contains("Name") || line.contains("Date of birth"))
.reduce((s, s2) -> {
var name = s.split("Name:")[1];
var birthday = s2.split("Date of birth:")[1];
return String.join(",", name, birthday);
}).get();
}
}