Skip to content

Commit a1d5b33

Browse files
committed
Example of Comparator and IO
1 parent ba52f13 commit a1d5b33

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/part4io/Comparator.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package part4io;
2+
3+
class Person {
4+
String lastName;
5+
6+
public String getLastName() {
7+
return lastName;
8+
}
9+
10+
public void setLastName(String lastName) {
11+
this.lastName = lastName;
12+
}
13+
}
14+
15+
public class Comparator {
16+
17+
public static void main(String[] args) {
18+
java.util.Comparator<Person> c = java.util.Comparator.comparing(Person::getLastName);
19+
//.thenComparing(Person::getFirstName)
20+
21+
//c.reversed(); Reversed
22+
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package part4io;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.stream.Stream;
8+
9+
public class ExploringDirectories {
10+
11+
public static void main(String[] args) throws IOException {
12+
Path p = Paths.get("c:", "Vodafone");
13+
14+
15+
try(Stream<Path> s = Files.list(p)) {
16+
s.filter(path -> path.toFile().isDirectory()).forEach(System.out::println);
17+
}
18+
19+
try(Stream<Path> s = Files.walk(p, 2)) {
20+
s.filter(path -> path.toFile().isDirectory()).forEach(System.out::println);
21+
}
22+
}
23+
}

src/part4io/ReadingTextFiles.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package part4io;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.stream.Stream;
8+
9+
public class ReadingTextFiles {
10+
11+
public static void main(String[] args) throws IOException {
12+
Path path = Paths.get("d:", "tmp", "file.txt");
13+
14+
try(Stream<String> s = Files.lines(path)) {
15+
s.filter(line -> line.contains("ERROR")).findFirst().ifPresent(System.out::println);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)