从.jar中读取字典文本文件 [英] Reading a Dictionary Text file from a .jar
本文介绍了从.jar中读取字典文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一个项目,并且已经给出了查看jar文件中的输入字符串是否在字典中的任务。在我的项目浏览器中,该字典可以在.jar下找到,如下所示:
Dictionaries.jar
| - > com.backwards.url
| - > WordLists.class
| ---> Dictionary.ListOne.txt
| ---> Dictionary.ListTwo.txt
有没有一个简单的方法来阅读这些字典,更重要的是看看是否有一个字? p>
字典文件本身只是一个单词列表,即:
apple
banana
clementine
...
解决方案
如果您需要更详细的回答,请随时告知我。
public void readDic(){
URI uri = null;
文件文件= null;
JarFile jarFile = null;
try {
uri = getClass()。getResource(/ resource / Dictionaries.jar)。toURI();
file = new File(uri);
jarFile = new JarFile(file);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(URISyntaxException e){
// TODO自动生成的catch块
e.printStackTrace();
}
if(jarFile!= null){
枚举< 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)){
//找到您的关键字
}
}
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
...
解决方案
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
}
}
这篇关于从.jar中读取字典文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文