In Java, I want to split an array into smaller arrays, the length of which varys with inputted text files

StackOverflow https://stackoverflow.com/questions/11289975

  •  18-06-2021
  •  | 
  •  

Question

So far, I have 2 arrays: one with stock codes and one with a list of file names. What I want to do is input the .txt files from each of the file names from the second array and then split this input into: 1. Arrays for each file 2. Arrays for each part with each file.

I have this:

            ImportFiles f1 = new ImportFiles("File"); 
    for (String file : FileArray.filearray) { 
        if (debug) {
            System.out.println(file);
        }
        try { 
            String line;
            String fileext = "C:\\ASCIIpdbSKJ\\"+file+".txt";
            importstart = new BufferedReader(new FileReader(fileext));
            for (line = importstart.readLine(); line != null; line = importstart.readLine()) {
                importarray.add (line);
                if (debug){
                    System.out.println(importarray.size());
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        importarray.add ("End")

This approach works to create a large array of all the files, will it be easier to change the input method to split it as it is coming in or split the large array I have?

At this point, the stock code array is irrelevant. Once I have split the arrays down I know where I will go from there.

Thanks.

Edit: I am aware that this code is incomplete in terms of { } but it is only printstreams and debugging missed off.

Was it helpful?

Solution

If you want to get a map with a filename and all its lines from all the files, here are relevant code parts:

Map<String, List<String>> fileLines = new HashMap<String, List<String>>();
for (String file : FileArray.filearray)
    BufferedReader reader = new BufferedReader(new FileReader(fileext));

    List<String> lines = new ArrayList<String>();
    while ((line = reader.readLine()) != null){
        lines.add(line);
    }

    fileLines.put(file, lines);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top