随机生成小写字母,并写入文件中
package work4;
import java.io.*;
import java.util.Random;
public class main {
public static void main(String []args){
File file = new File("./src/work4/test.txt");
if(!file.exists()){
try {
file.createNewFile();
System.out.println("创建成功");
}
catch (IOException exp){}
}
try{
FileWriter w = new FileWriter(file); //创建一个对象
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 20; j++){ //循环输入20个字母
char x = getLetter();
w.write(x);
}
w.write("\n"); //换行
}
w.close();
}
catch (IOException exp){}
}
public static char getLetter(){
Random x = new Random();
char someone = (char)(x.nextInt(122-97+1)+97); //随机返回一个小写字母
return someone;
}
}