简体   繁体   中英

reading files recursively and storing them java

    public Vector readFiles(File Mydir) {

        Vector<File> vec = new Vector<>(10,10);  // to store the needed files

        for (File f: Mydir.listFiles()){
            if (!f.isDirectory()){
                System.out.println("file found"+ f.getName());
                vec.addElement(f);

                // reads all the files in the directory recursively
            } else
                readFiles(f);
        }
       // System.out.println("size of vec = " + vec.size());
        return vec;
    }

Hi I'm trying to read all txt and pdf files in a Directory and store them in a vector . but my vector only adds 4 of them as an element ! I printed the files and they are all found but not added to the vec . thanks for your help

When you call recursively your method, you don't provide the valued vector :

   readFiles(f);


It is overwritten at each call :

public Vector readFiles(File Mydir) {

    Vector<File> vec = new Vector<>(10,10);  // to store the needed files

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