根据小写26个字母,随机生成单词,乱文
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
/**
* @author Mxhlin
* @Email [email protected]
* @Date 2022/09/22/19:27
* @Version
* @Description 根据小写26个字母,随机生成单词,乱文
*/
public class OutputStreamDemo {
public static void main(String[] args) {
Random rand = new Random();
try (FileOutputStream fos = new FileOutputStream("木.txt")){
for (int i = 0; i < 100; i++) {
fos.write(randStr(rand.nextInt(2,20)).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() ;
}
}
标签:rand,26,乱文,int,Random,ts,小写,new,String
From: https://www.cnblogs.com/xhlin/p/16721484.html