code: public void processTreebankWithHeadWords(final File dir) throws IOException {
      // process one file
      if (dir.isFile()) {
        final List<String> inputTrees = Files.readLines(
            new File(dir.getCanonicalPath()), Charsets.UTF_8);
        final File outfile = new File(
            Files.getNameWithoutExtension(dir.getPath()) + ".head");
        final String outTree = addHeadWordsToTreebank(inputTrees);
        Files.write(outTree, outfile, Charsets.UTF_8);
        System.err.println(">> Wrote headWords to " + outfile);
      } else {
        // recursively process directories
        final File listFile[] = dir.listFiles();
        if (listFile != null) {
          for (final File element : listFile) {
            if (element.isDirectory()) {
              processTreebankWithHeadWords(element);
            } else {
              try {
                final List<String> inputTrees = Files.readLines(
                    new File(Files.getNameWithoutExtension(element
                        .getCanonicalPath())), Charsets.UTF_8);
                final File outfile = new File(
                    Files.getNameWithoutExtension(element.getPath()) + ".head");
                final String outTree = addHeadWordsToTreebank(inputTrees);
                Files.write(outTree, outfile, Charsets.UTF_8);
                System.err.println(">> Wrote headWords to " + outfile);
              } catch (final FileNotFoundException noFile) {
                continue;
              }
            }
          }
        }
      }
    }
docs: Takes a file containing Penn Treebank oneline annotation and annotates the
  headwords, saving it to a file with the *.head extension. Optionally also
  processes recursively an input directory adding heads only to the files
  with the files with the specified extension.
  
  @param dir
  the input file or directory
  @throws IOException
  if io error