0

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.

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by ksaxena
0

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.

0

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]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.