-1

I have written a java code for counting the instruction frequency using hashmap. Now I want to run this file for a whole folder at once which has several .txt files.

class HashMapCount
{
    public static void main (String[] args) throws java.lang.Exception
    {
       HashMap h=new HashMap();                        
       FileInputStream fin=new FileInputStream("C:\\test.txt");
       BufferedReader br=new BufferedReader(new InputStreamReader(fin));
       String n;
         while((n=br.readLine())!=null)
               {
                   if(h.containsKey(n))
                         {
                           int i=(Integer)h.get(n);
                           h.put(n,(i+1));
                         }
                   else
                           h.put(n, 1);
                }

    Set setOfKeys = h.keySet();

     Iterator iterator = setOfKeys.iterator();

    while (iterator.hasNext()) 
         {
             String key = (String) iterator.next();

             Integer value = (Integer)h.get(key);

             System.out.println("Key: "+ key+", Value: "+ value);    
          }
  }
}

2 Answers2

1

You can use an Iterator to traverse the files in your folder, like so

File folder = new File("/"); // path of folder
File[] files = folder.listFiles();

for (File file : files) {
    if (file.isFile() && file.endsWith(".txt")) {
        // your file handling logic here
    }
}
svarog
  • 9,477
  • 4
  • 61
  • 77
  • I want to use a single hasmap for all the files. So where do I use the Iterator for file traversal in my code snippet? – Siddharth Kota May 03 '15 at 10:32
  • the for loop is the iterator, see: http://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html , inside the iterator do the hashmap operation – svarog May 03 '15 at 10:36
0
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class IsFolderSubdirectory {

    public static HashMap h = new HashMap();

    public static void addtoHashMap(File filename) {

        FileInputStream fin;
        try {
            fin = new FileInputStream(filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            String n;
            try {
                while ((n = br.readLine()) != null) {
                    if (h.containsKey(n)) {
                        int i = (Integer) h.get(n);
                        h.put(n, (i + 1));
                    } else
                        h.put(n, 1);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void isfolder(File findfile) {
        if (findfile.isDirectory()) {
            System.out.println(findfile + "is a directory");
            String[] directoryContents = findfile.list();
            for (int i = 0; i < directoryContents.length; i++) {
                File newpath = new File(findfile + "/" + directoryContents[i]);
                isfolder(newpath);
            }
        } else {
            IsFolderSubdirectory.addtoHashMap(findfile);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String pathname = "GIVE YOUR PATH HERE";
        File file = new File(pathname);
        String[] directoryContents = file.list();
        IsFolderSubdirectory ifs = new IsFolderSubdirectory();

        for (int i = 0; i < directoryContents.length; i++) {
            File newpath = new File(pathname + "/" + directoryContents[i]);
            ifs.isfolder(newpath);
        }

    }
}

Check the folder consist files or folder. If file found call addtoHashMap to add it in hashmap else iterate the same till it find the file. Repeat the same for all child directories exist and the files present in it.

Nirmal
  • 1,229
  • 1
  • 15
  • 31
Mohan Raj
  • 1,104
  • 9
  • 17
  • Thankyou for the answer but the actual function of my hasmap was to count the frequencies and not storing file name. I just want to use a single hash map to count frequencies of instructions from all the files. – Siddharth Kota May 03 '15 at 10:41
  • I have use the staic hashmap so that it will use the same hashmap for all files. – Mohan Raj May 03 '15 at 10:52