编程程序实现使用io流,在本地建立一个文本文件,文件内容是5000行字符英文字符串,每行有3-15 随机字母组成的词汇,第一个词汇首字母大写,每个词汇之间用空格隔开,最后一个词汇尾部加上点

编程程序实现使用io流,在本地建立一个文本文件,文件内容是5000行字符英文字符串,每行有3-15 随机字母组成的词汇,第一个词汇首字母大写,每个词汇之间用空格隔开,最后一个词汇尾部加上点

package cha;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class aaaa {
        public static void main(String[] args) {
            Random rand = new Random();
            try (FileOutputStream fos = new FileOutputStream("张.txt")){
                for (int i = 0; i < 5000; i++) {
                    fos.write(randStr(rand.nextInt(3,15)).concat("\t\n").getBytes());
                }
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        public static String randStr(int n) {
            Random rand = new Random();
            String letter = "abcdefghijklmnopqrstuvwsyz";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                StringBuilder sb1 = new StringBuilder();
                int len = rand.nextInt(2,10);// 随机单词的长度
                for (int j = 0; j < len; j++) {// 给单词赋值
                    sb1.append(letter.charAt(rand.nextInt(letter.length())));
                }
                sb1.append(" ");// 后面添加空格

                String ts = new String(sb1);
                if (i ==0 ){
                    // 首字母大写
                    ts = ts.substring(0,1).toUpperCase().concat(ts.substring(1));
                }else if (i == n-1){
                    // 结尾符号
                    ts = ts.substring(0,ts.length()-1)+"!.?".charAt(rand.nextInt(3));
                }
                sb.append(ts);
            }
            return sb.toString()    ;
        }
    }

	





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值