word.txt有v,n,adj
verb:eat,adjective:terrible,verb:drink,verb:sleep,verb:play,adjective:wonderful,noun:rice,noun:meat,noun:hand,noun:hair,adjective:beautiful,adjective:expensive,adjective:horrible,adjective:lovely,adjective:silly,adjective:red,adjective:black,adjective:purple,adjective:pink,adjective:brown
将不同词性的单词归类
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
public class Word {
public static void main(String[] args) {
FileReader fr= null;
String str = null;
String verbs = "", nouns = "",adjective="";
String v = "verb", n = "noun",a="adjective";//参照字符串
try {
fr = new FileReader("src//word.txt");
char[] car=new char[1024];
int len=0;
while ((len= fr.read(car))!=-1) {
str=new String(car,0,len);
}
str = str.replace(",", " ");
//split分割 \\s+一个或多个空格
String[] strs = str.split("\\s+");
//把同词性的单词拼接到一个字符串中
for (int i = 0; i < strs.length; i++) {
if (strs[i].contains(v)) {
verbs += strs[i];
verbs += " ";
}
if (strs[i].contains(n)) {
nouns += strs[i];
nouns += " ";
}
if (strs[i].contains(a)) {
adjective += strs[i];
adjective += " ";
}
}
verbs =verbs.replace("verb:", "");
adjective =adjective.replace("adjective:", "");
nouns =nouns.replace("noun:", "");
// 创建文件夹 file
File dir = new File("file");
if(!dir.exists()){
Files.createDirectory(dir.toPath());
}
//把数据写到外存
FileOutputStream out = new FileOutputStream("file/verb.txt");
out.write(verbs.getBytes());
out = new FileOutputStream("file/noun.txt");
out.write(nouns.getBytes());
out = new FileOutputStream("file/adjective.txt");
out.write(adjective.getBytes());
fr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}