7

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?

0

2 Answers 2

6
Charset charset = Charset.forName("UTF-8");
List<String> list = new ArrayList<String>();
try(
  FileInputStream is1=new FileInputStream("sum1.csv");
  FileInputStream is2=new FileInputStream("sum2.csv");
  SequenceInputStream is=new SequenceInputStream(is1, is2);
  BufferedReader reader=new BufferedReader(new InputStreamReader(is, charset));)
{
  try {
      String line;
      while((line = reader.readLine()) != null && !line.isEmpty()){
          list.add(line);
      }
  } catch (IOException e) {
      System.err.format("IOException: %s%n", e);
  }
}

By the way, did you mean

String line;
while((line = reader.readLine()) != null)
  if(!line.isEmpty()) list.add(line);

for your inner loop? Your code stops at the first empty line, my suggested alternative skips empty lines. But I can only guess your real intention.

Sign up to request clarification or add additional context in comments.

5 Comments

I have somewhat similar scenario. It's just that I have sum1 to sumN, N is a very huge number. How can I read all of them together at a time? Can I do something like sum*?
@tanmay2507: SequenceInputStream has a constructor that accepts an Enumeration. If you struggle building a solution upon that, open a new question.
Ugh, I wish there was a way to pass it via lambdas.
@tanmayghosh2507 is probably looking for Collections.enumeration(List.of(is1, is2))
@SridharSarnobat maybe this answer helps. Its StreamInputStream accepts a Stream which you can populate with lambdas.
1

In the code above, you did create a new BufferedReader to read from the second file. What you've done is perfectly fine, although it would make sense to put the repeated code into a method that takes the filename and the list of strings as arguments.

You've got one little glitch - if there's an empty line in the middle of either of your files, your program stops reading when it gets to it. I'm not sure if that's actually what you want.

2 Comments

It seems no way to avoid creating a new BufferedReader to read from the second file. What do you think?
I certainly don't know of any way to avoid it. But I also don't know why you would want to avoid it. Having a new BufferedReader works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.