java输入一个学生信息,形成一个文件,再读出并显示在屏幕上
1 //输入一个学生信息,形成一个文件,再读出并显示在屏幕上 2 package classwork10; 3 4 import java.io.FileNotFoundException; 5 import java.io.PrintWriter; 6 import java.util.Scanner; 7 8 public class student_7 { 9 private String name; 10 private String id; 11 private double score; 12 public student_7() { 13 Scanner in=new Scanner(System.in); 14 System.out.println("请输入学生姓名,学号和成绩:"); 15 String name1=in.next(); 16 String id1=in.next(); 17 double score1=in.nextDouble(); 18 set(name1, id1,score1); 19 } 20 public void set(String name, String id, double score) { 21 this.name = name; 22 this.id = id; 23 this.score = score; 24 } 25 @Override 26 public String toString() { 27 return "name=" + name + ", id=" + id + ", score=" + score; 28 } 29 public void write() throws FileNotFoundException { 30 PrintWriter pw=new PrintWriter("d:/xyz.txt"); 31 pw.println(toString()); 32 pw.close(); 33 } 34 public void output() { 35 System.out.println(toString()); 36 } 37 } 38 39 40 41 42 package classwork10; 43 44 import java.io.FileNotFoundException; 45 46 public class student_7testt { 47 48 public static void main(String[] args) throws FileNotFoundException { 49 student_7 a=new student_7(); 50 a.output(); 51 a.write(); 52 } 53 54 }
道阻且长,行则将至