📄 buildwordvacabulary.java
字号:
package com.luceneheritrixbook.core;
import java.io.*;
import java.text.Collator;
import java.util.*;
public class BuildWordVacabulary {
private String wordvacabularypath = null;
private ArrayList wordlist = new ArrayList();
public void loadProductFromDirectory(String[] dirs) throws IOException {
String fullname = null;
try {
if (wordvacabularypath == null) {
throw new IOException("词库存放的文件没有指定,无法生成!");
}
for (int i = 0; i < dirs.length; i++) {
String folder = dirs[i];
File foo = new File(folder);
String[] files = foo.list();
for (int j = 0; j < files.length; j++) {
File f = new File(foo, files[j]);
fullname = f.getName();
// 第一步取得产品的名称
// 第二步取得产品的型号
System.out.println(fullname);
if (fullname.indexOf("-") == fullname.lastIndexOf("-")) {
continue;
}
String type = fullname.substring(fullname.indexOf("-") + 1,
fullname.lastIndexOf("-"));
String name = fullname.substring(0, fullname.indexOf("-"));
if (!wordlist.contains(type)) {
wordlist.add(type);
}
if (!wordlist.contains(name)) {
wordlist.add(name);
}
}
}
Collections.sort(wordlist);
writeToFile();
} catch (Exception e) {
System.out.println(fullname);
e.printStackTrace();
}
}
private void writeToFile() throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(
wordvacabularypath));
for (int i = 0; i < wordlist.size(); i++) {
writer.write((String) wordlist.get(i));
writer.newLine();
}
writer.close();
}
public void setWordvacabularyPath(String wordvacabularypath) {
this.wordvacabularypath = wordvacabularypath;
}
public static void main(String[] args) throws IOException {
BuildWordVacabulary builder = new BuildWordVacabulary();
builder.setWordvacabularyPath("e:\\word.txt");
builder.loadProductFromDirectory(new String[] { "c:\\product\\mobile\\" });
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -