思路

查看代码注解

实现

代码

package fileWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class fileWriter {
public static void main(String[] args) throws IOException {
	File file=null;
	FileWriter fw=null;
	file=new File("d:\\abc.txt");
	Scanner scanner=new Scanner(System.in);
	String name;
	String gerden;
	String banji;
	if (!file.exists()) {
	file.createNewFile();
	}
	fw=new FileWriter(file,true);
	for(int i=1;i<=2;i++) {
		while(true) {
			System.out.println("請輸入學生姓名:");
			name=scanner.next();
			System.out.println("請輸入學生性別:");
			gerden=scanner.next();
			System.out.println("請輸入學生班級:");
			banji=scanner.next();
			fw.write(i+":學生姓名: "+name+"\r\n"+"   學生性別: "+gerden+"\r\n"+"   學生班級: "+banji+"\r\n");
			fw.write(""+"\r\n");
			break;
		}
		
		}
	fw.close();
	}
	}