以隨機產生大寫字母爲例,執行代碼後文檔中有n行m列大寫數字
以下爲示例代碼
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class filewr {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Random r1 = new Random(); //隨機數
r1.setSeed(100);
File f1=new File("C:/Users/ASUS/Documents/java/Ten","f11.text");//創建文件對象
if(!f1.exists()){//判斷文件是否存在
try {
f1.createNewFile();//若不存在則創建文件
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileWriter fw = new FileWriter(f1.getPath());//得到所寫文件的路徑
int n = sc.nextInt();//輸入行
int m = sc.nextInt();//輸入列
for (int i = 1; i <= n; i++) {
String s = "";
//代碼填空第1行,m作爲循環變量的循環結構
for (int a = 1; a <= m; a++){
//代碼填空第2行,給x賦值,x爲65到90之間的一個隨機整數
x=r1.nextInt(26)+65;
s = s + (char)x;
}
fw.write(s+"\n");//輸入到文件中
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以下爲執行代碼後文檔中的內容