简体   繁体   中英

java Reading list of list of files

Can someone help me with reading a list of list of csv files. Like

List<List<File>> filesList;

Want to read through all the files contents in each list for processing, but unable to comeup with loop structure which can be used. Thanks.

UPDATE: I want to load files from each inner file list simultaneously. Like read first file from each inner list at a time, compare contents, then move to second file of a particular list and so on. Each inner list can be of variable size.

Here's an appropriate loop for this:

for (List<File> innerList : filesList) 
    for (File file : innerList) 
        // do something for a file

Alternatively you can use Iterator ,like this

    List<ArrayList<File>> filesList = new ArrayList<ArrayList<File>>();
    //add objects to filesList here

    Iterator<ArrayList<File>> filesListIterator = filesList.iterator();

    while(filesListIterator.hasNext())
    {
        ArrayList<File> files = filesListIterator.next();

        Iterator<File> filesIterator = files.iterator();

        while(filesIterator.hasNext())
        {
            File file = filesIterator.next();
            //do your own logic here;
        }
    }  


This may help you for compare

 while(filesListIterator.hasNext())
    {
        ArrayList<File> files = filesListIterator.next();

        for(int i=0;i<files.size()-1;i++)
        {
            File firstFile = files.get(i);//get a file

            File secondFile = files.get(i+1);//get the next file

            compareFiles(firstFile,secondFile);//this is your defined   
                                             //method for compare
        }
    } 
    List<List<File>> filesList;
    for (List<File> list : filesList) {
        for (File file : list) {
            // your code here ...
        }
    }

Personally I would recommend using Google Guava if you can, this has a concat method

    final List<List<File>> files = Lists.newArrayList();
    for (final File file : Iterables.concat(files)) {
        //doStuff with file
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM