I'm sure this has been discussed before, but a forum search didn't do me much good, nor did a google search.

I want to look into a folder and iterate through the text files in it....how do I do so.

Recommended Answers

All 2 Replies

Look into the documentation of the File class and try something on your own. Post your code if you still aren't able to figure out things.

Hope code below will help:

[INDENT]package miscellaneous;

import java.io.File;

/**
 * 
 * @author kapil saxena
 *
 */
public class FolderIterator {
    [INDENT]public static void main(String[] args) 
    {
        [INDENT]String path = "E:/atao_head";
        searchFolders(new File(path));
        System.out.println("Done");[/INDENT]        
    }
    /**
     * 
     * @param fo - File object received recursively.
     */
    public static void searchFolders(File fo)
    {
        [INDENT]if(fo.isDirectory())
        {
            [INDENT]System.out.println("Searching in..." + fo.getName());
            String internalNames[] = fo.list();
            for(int i=0; i<internalNames.length; i++)
            {
                searchFolders(new File(fo.getAbsolutePath() + "\\" + internalNames[i]));
            }[/INDENT]        }
        else
        {
            System.out.println("reached a file..." + fo.getName());
        }[/INDENT]    }[/INDENT]}

[/INDENT]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.