Reputation: 2205
Consider I want to println the lines of a list of files, using the Java 8 streams API only. How would I write that?
This is not my real case, just a very simplified form of it.
File[] files;
Arrays.stream(files). // what now?
I thought about mapping it from File
to Stream<String>
but then I got stuck.
Upvotes: 1
Views: 180
Reputation: 298579
public class AllFilesLines {
public static void main(String[] args) {
File[] files = …
Arrays.stream(files).flatMap(AllFilesLines::lines)
.forEach(System.out::println);
}
static Stream<String> lines(File f) {
try { return Files.lines(f.toPath()); }
catch (IOException e) { throw new UncheckedIOException(e); }
}
}
Upvotes: 6
Reputation: 93892
You can also use Files.lines
.
By default it considers that the characters in the file are encoded in UTF-8, but you can specify a Charset
if you want.
File[] files = {new File("file1.txt"), new File("file2.txt")};
for(File f : files){
Files.lines(f.toPath()).forEach(System.out::println);
}
Upvotes: 1
Reputation:
You can do something like this.
package de.lhorn.so;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
public class SOPlayground {
public static void main(String[] args) throws Exception {
File[] files = {new File("/tmp/1.txt"), new File("/tmp/2.txt")};
Arrays.stream(files).forEach(f -> { // Stream of File
try (InputStream fis = new FileInputStream(f);
Reader isr = new InputStreamReader(fis);
BufferedReader reader = new BufferedReader(isr)) {
reader.lines().forEach(line -> { // Stream of String
System.out.println(line);
});
} catch (IOException ex) {
System.err.println(ex);
}
});
}
}
Edit: Using Files
:
package de.lhorn.so;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class SOPlayground {
public static void main(String[] args) throws Exception {
File[] files = {new File("/tmp/1.txt"), new File("/tmp/2.txt")};
Arrays.stream(files).forEach(f -> {
try {
Files.lines(f.toPath()).forEach(System.out::println);
} catch (IOException ex) {
System.err.println(ex);
}
});
}
}
Upvotes: 1