Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:00:24+00:00 2026-06-02T03:00:24+00:00

I want to iterate over really many files which are placed in a deep

  • 0

I want to iterate over really many files which are placed in a deep folder hierarchy.
The files in question are 15 GB of MS Word documents I intend to process with POI. POI works fine, but a simple recursive function creates an OutOfMemoryException:

public void checkDir(File dir) {
    for (File child : dir.listFiles()) {
        if (".".equals(child.getName()) || "..".equals(child.getName()))
            continue; // Ignore the self and parent aliases.
        if (child.isFile())
            processFile(child); // do something
        else if (child.isDirectory())
            checkDir(child);
    }
}

// check if the word file can be read by POI
private void processFile(File file) {
InputStream in = null;
try {
    in = new FileInputStream(file);
    WordExtractor extractor = null;

    try {
        extractor = new WordExtractor(in);
        extractor.getText();
    } catch (Exception e) {
        // This can happen if the file has the "doc" extension, but is
        // not a Word document
        throw new Exception(file + "is not a doc");
    } finally {
        in.close();
        in = null;
    }
} catch (Exception e) {
    // log the error to a file
    FileWriter fw = null;
    try {
        fw = new FileWriter("corruptFiles.txt", true);
        fw.write(file.getAbsolutePath() + "\r\n");
    } catch (Exception e2) {
        e.printStackTrace();
    } finally {
        try {
            fw.close();
        } catch (IOException e3) {
        }
    }       
}

Trying to accomplish this with org.apache.commons.io.FileUtils.iterateFiles yields the same exception:

String[] extensions = { "doc" };

Iterator<File> iter = FileUtils.iterateFiles(dir, extensions, true);
for(; iter.hasNext();)
{
    File file = iter.next();
    processFile(file); // do something
}

I am running Java 6 on Windows 7 and not allowed to move or rearrange the files.

What are my options?

Thank you for your time.

[EDIT] Added the processFile function. Just did a successful run with a simplified version of processFile after increasing the heap size to 512 MB.
In conclusion my problem is somehow POI related and NOT to iterating files.

private void processFile(File file) {
    System.out.println(file);
}

[EDIT2] I could narrow the cause of the exception down to a 33 MB file. Trying to parse that results in the java.lang.OutOfMemoryError: Java heap space exception. I will post a ticket to the POI bug tracker. Thanks everybody for your suggestions.
I’ll accept MathAsmLang’s answer as that helped to overcome the iteration problem.
I would have accepted krishnakumarp’s comment as an answer, as he was the first one to point that out, but that proved to be impossible 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:00:27+00:00Added an answer on June 2, 2026 at 3:00 am

    Because it is outofmemoryerror, you should try out starting jvm with
    different memory parameters i.e. heap size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I really want to do is iterate over the keys of a stringmap.
I want to iterate over a sequence in xquery and grab 2 elements at
I want to iterate over an array of inputs that belong to certain class
I want to iterate over all childs of a jQuery's .children() return value, like
I want to iterate over checkboxes div and determine if checkbox1,checkbox2,checkbox3 are checked //
I have a boost::bimap and I want to iterate over all positions to add
I have a Hashtable in Java and want to iterate over all the values
I'm using Richfaces JSF and I want to iterate over an Map<Object,Object> . I
I have 3 (Edit) mutually exclusive IEnumerables that I want to iterate over. I
I have a String with some value. I want to iterate over all classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.