0

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.

  • Could you expand on what you mean by "1. Arrays for each file 2. Arrays for each part with each file"? It's not clear to me which arrays you want to create per file (currently you're just adding each line within the file), or what the "parts" within the files are. – Andrzej Doyle Jul 02 '12 at 08:43
  • From the arrays I have, I want to input files which have descriptions of several parts. Each part has various properties associated with it, so the descriptions length varies. I want to create arrays of each inputted file from the filearray and then split the created arrays into arrays of part descriptions. Sorry for the confusion. –  Jul 02 '12 at 08:46

1 Answers1

0

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);
}
d1e
  • 6,372
  • 2
  • 28
  • 41
  • How is a HashMap's size limited? I was unsure as to it's function so looked into it and found references to 'initial capacity'. http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html –  Jul 02 '12 at 09:13
  • @SeanKenny its capacity is dynamic. Is thats what you wanted to achieve? – d1e Jul 02 '12 at 10:30
  • 1
    Not exactly, I was just exploring that option. I think I have found a solution. –  Jul 02 '12 at 11:32
  • Eventually, thanks. Not for the same thing but helpful all the same. –  Jul 13 '12 at 07:29