I want to read texts from two or more files using a single BufferedReader object.
This is how I did it in my code.
Charset charset = Charset.forName("UTF-8");
Path p1 = Paths.get("sum1.csv");
List<String> list = new ArrayList<String>();
BufferedReader reader = Files.newBufferedReader(p1, charset);
try {
String line;
while((line = reader.readLine()) != null && !line.isEmpty()){
list.add(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
reader.close();
}
Path p2 = Paths.get("sum2.csv");
reader = Files.newBufferedReader(p2, charset);
try {
String line;
while((line = reader.readLine()) != null && !line.isEmpty()){
list.add(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
reader.close();
}
The code compiled and run correctly.
What is the standard way to deal with this problem? Is it possible to read two or more files using a single BufferedReader?