muktoshuvro
muktoshuvro

Reputation: 438

File[] can not handle multiple files

I want to read multiple files from multiple directories. So far my program can read multiple files only until certain limit.

 public void filesTobeCollected(String validpath) throws Exception {
    this.folder = new File(validpath);
    //  this.file = new ArrayList<>();
    File[] fileEntry = folder.listFiles();
    System.out.println("directory path" + fileEntry.length);

    /*****File upload read****/

    for (int i = 0; i < fileEntry.length; i++) {

        /*this line weeds out other directories/folders*/
        if (fileEntry[i].exists()) {

            file.add(String.valueOf(fileEntry[i]));
          }
      }
    }

and I send value directories through this function,

  public List<String> filesTobeTested(ArrayList<String> file) throws   
  Exception {
   Set<String> test = new HashSet<>();
    test.add("src/test/resources/gfd");
    test.add("src/test/resources/rds");
    test.add("src/test/resources/oiu");
    //test.add("src/test/resources/pol");

    System.out.println("directory path 2" + test);

    for (int i = 0; i < 1; i++) {

        for (Iterator<String> it = test.iterator(); it.hasNext(); ) {
            String f = it.next();
            filesTobeCollected(f);
            System.out.println("Found" + f);
        }

Here File[] fileEntry seems to have some limit as I push more directories using test.add("src/test/resources/obj") File[] fileEntry can't handle any more files. Is any alternative for File[] fileEntry incase of unlimited or unknown number of files?

Upvotes: 1

Views: 74

Answers (2)

ArifMustafa
ArifMustafa

Reputation: 4965

As deeply I recognized you, you want to collect those files in one.

In my case first I would recommend to collect the file(s) path or file(s) and add those in an ArrayList<File>, secondary read those file(s) and collect in ArrayList<byte[]>, later if you want to make it a single file, Surely you can use an ObjectOutputStream class to write the ArrayList<byte[]> Object and certainly create a single File

During this take care of File reading & writing bytes sizes don't crosses your Java Heap Space boundary.

Upvotes: 1

Jordi Castilla
Jordi Castilla

Reputation: 26981

Is any alternative for File[] fileEntry incase of unlimited or unknown number of files?

Yes, use ArrayList<File> wich will handle size automatically for you.


But this does not seem to be the problem because you know all the files you need to handle in the array creation so size is accurate File[] fileEntry = folder.listFiles();.

Remove this line

for (int i = 0; i < 1; i++) {

which seems useles... If problem remains, post your stacktrace

Upvotes: 3

Related Questions