蓝桥杯 单词归类

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪八戒1.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值