0

I am working on a project and have been given the task of seeing if an input string is in a dictionary from a jar file. In my project explorer the dictionary can be found under the .jar like this:

Dictionaries.jar
|-->com.backwards.url
    |-->WordLists.class
        |--->Dictionary.ListOne.txt
        |--->Dictionary.ListTwo.txt

Is there an easy way to read these dictionaries and more importantly see if a word is in them?

The dictionary files themselves are just a list of words, i.e:

apple
banana
clementine
...
Evan
  • 572
  • 2
  • 7
  • 29
  • This should be of some help for accessing a file inside your classpath : http://stackoverflow.com/questions/793213/getting-the-inputstream-from-a-classpath-resource-xml-file?lq=1 – GPI Apr 13 '16 at 15:24
  • Do you want to access *JAR*'s content from [Java's code](http://stackoverflow.com/questions/676097/java-resource-as-file) or [from bash](http://stackoverflow.com/questions/7066063/how-to-read-manifest-mf-file-from-jar-using-bash)? – patryk.beza Apr 13 '16 at 15:30
  • Within my code. I would like to make a function that checks if a word is on these lists and then handle it accordingly – Evan Apr 13 '16 at 15:35
  • This has been asked before, did you bother reading either of the links already posted here? Those show you how to access the files, we aren't going to write your logic for the checks. – redFIVE Apr 13 '16 at 15:38

1 Answers1

0

Feel free to tell me if you need more detailed answer.

public void readDic() {     
    URI uri = null;
    File file = null;
    JarFile jarFile = null;

    try {
        uri = getClass().getResource("/resource/Dictionaries.jar").toURI();
        file = new File(uri);
        jarFile = new JarFile(file);            

    } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if(jarFile != null) {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            this.findKeyWord(entries.nextElement());
        }
    }

}

private void findKeyWord(JarEntry entry) {
       String name = entry.getName();
       System.out.println(name);

       if(name.endsWith(".txt")) {
         //Find your keyword
       }

}
Tri Nguyen
  • 56
  • 5