品詞によると

単語の配列。配列内の各文字列の形式は「品詞:単語」です。

  • String [] words = {"verb:eat"、 "verb:drink"、 "verb:sleep"、 "verb:play"、 "noun:rice"、
    "noun:meat"、 "noun:hand"、 "noun :hair”};
    単語の性質に応じて、動詞はすべてverb.txtファイルに保存されます。
    単語の性質に応じて、名詞はすべてnoun.txtファイルに保存されます。
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();
    }
}

おすすめ

転載: blog.csdn.net/qq_43496435/article/details/113916296