導入Java . io . *;
導入Java . util . *;
公共類測試{
公共靜態void main (String[] args) {
嘗試{
字符串strName =
String strSex =
int I = 1;
//構造寫入文件的File對象。
File file2 =新文件(" a . txt ");//創建壹個a.txt文件。
//構造壹個低級輸出流
FileWriter fw =新文件寫入器(文件2);
//構造高級輸出流
buffered writer bw = new buffered writer(fw);
while(true){
System.out.println("請輸入"+i+"學生姓名:");
Scanner sc =新掃描儀(system . in);
StrName = " Name:"+sc . next();
System.out.println("請輸入性別:");
Scanner sc2 =新掃描儀(system . in);
StrSex = " gender "+sc2 . next();
//寫文件
bw . write(strName+" "+strSex);
bw . newline();//換行
System.out.println("是否要結束程序:y/n ");
Scanner sc3 =新掃描儀(system . in);
string str = sc3 . next();
if(str . equals(" y "))break;//如果輸入Y,則退出程序。
i++;
}
//關閉流
bw . close();
fw . close();
}catch(IOException ex1){
System.out.println("輸入輸出異常");
}
System.out.println("程序結束");
}
}
剛開始學java,可能寫的不是很好..希望能幫到妳。..
缺點請見諒!