According to part of speech

An array of words, the format of each string in the array is "part of speech: word"

  • String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice",
    "noun:meat","noun:hand","noun :hair”};
    According to the nature of the word, verbs are all stored in the verb.txt file.
    According to the nature of the word, nouns are all stored in the noun.txt file.
import java.io.FileOutputStream;
import java.io.IOException;

public class Work2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        String[] words = {
    
    "verb:eat", "verb:drink", "verb:sleep", "verb:play",
                "noun:rice", "noun:meat", "noun:hand", "noun:hair"};
        //用String类里的contains判断当前字符串包含noun还是verb
        //把同词性的单词拼接到一个字符串中
        String verbs = "", nouns = "";//最终字符串
        String v = "verb", n = "noun";//参照字符串
        for (int i = 0; i < words.length; i++) {
    
    
            if (words[i].contains(v)) {
    
    
                verbs += words[i];
                verbs += "\n";//为了复制到文件能自动换行
            }
            if (words[i].contains(n)) {
    
    
                nouns += words[i];
                nouns += "\n";
            }
        }
        //jvm把数据写到外存
        FileOutputStream out = new FileOutputStream("verb.txt");
        out.write(verbs.getBytes());
        out = new FileOutputStream("noun.txt");
        out.write(nouns.getBytes());
        //资源释放
        out.close();
    }
}

Guess you like

Origin blog.csdn.net/qq_43496435/article/details/113916296