Search code examples
javaarraysbufferedreadershrink

How to create one BufferedReader for several text files


I am trying to read text from three separate files. I couldn't think of a cleaner way to use only one BufferedReader. And is there a way to save text in to a empty part of an array without creating byte i = 0. I am new to Java, So thanks for answering in advance.

        String unitsPath = "SI_system_units.txt";
    String namesPath = "SI_system_units_names.txt";
    String definitionsPath = "SI_system_definitions.txt";
    String line;
    byte i = 0;
    String[] siUnits = new String[100];
    String[] siNames = new String[100];
    String[] siDefinitions = new String[100];

    BufferedReader unitsBr = new BufferedReader(new FileReader(unitsPath));
    BufferedReader namesBr = new BufferedReader(new FileReader(namesPath));
    BufferedReader definitionsBr = new BufferedReader(new FileReader(definitionsPath));

    while ((line = unitsBr.readLine()) != null) {
        siUnits[i] = line.trim();
        siNames[i] = namesBr.readLine().trim();
        siDefinitions[i] = definitionsBr.readLine().trim();
        i++;
    }

Solution

  • Can I use a single BufferedReader for multiple files?

    Not really. A single BufferedReader can only operate with a single file.

    Is there a cleaner way to do this?

    There are some ways I can think of some ways to make the code a bit cleaner, but they are honestly overkill for what you are doing.

    Is there a way to save text in to an empty part of an array without creating an index pointer (byte i = 0)?

    No. Arrays are fixed-length and you are required to specify exactly where you want the value to be placed. If you want a dynamically-sized array, one which grows as needed, you can use an ArrayList.

    Note: Arrays are probably the most fundamental data structure and you need to be comfortable working with them. Many things are built using arrays including ArrayLists. ArrayLists do not remove the need for arrays.

    import java.util.ArrayList;  // import statement is required
    
    // ...snip...
    
    String unitsPath = "SI_system_units.txt";
    String namesPath = "SI_system_units_names.txt";
    String definitionsPath = "SI_system_definitions.txt";
    String line;
    ArrayList<String> siUnits = new ArrayList<>();
    ArrayList<String> siNames = new ArrayList<>();
    ArrayList<String> siDefinitions = new ArrayList<>();
    
    BufferedReader unitsBr = new BufferedReader(new FileReader(unitsPath));
    BufferedReader namesBr = new BufferedReader(new FileReader(namesPath));
    BufferedReader definitionsBr = new BufferedReader(new FileReader(definitionsPath));
    
    while ((line = unitsBr.readLine()) != null) {
        siUnits.add(line.trim());
        siNames.add(namesBr.readLine().trim());
        siDefinitions.add(definitionsBr.readLine().trim());
    }
    
    // Don't forget to close your files :)
    unitsBr.close();
    namesBr.close();
    definitionsBr.close();
    

    It is possible to go from ArrayList to array and back if needed, but that is a story for another time.