编写一个JAVA程序,将自己的个人信息(学号、姓名、性别等)保存到c:\myinfo.txt文件中。

如题所述

public class Util {
public static void main(String[] args) throws java.io.IOException{
//编写一个JAVA程序,将自己的个人信息(学号、姓名、性别等)保存到c:\myinfo.txt文件中。
Person p1 = new Person("0001","小明",22);
java.io.File file = new java.io.File("C:\\myinfo.txt");
java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(file)));
bw.write("学号:" + p1.id);
bw.write("\r\n姓名:" + p1.name);
bw.write("\r\n性别:" + p1.age);
bw.flush();
bw.close();
}
}
class Person{
public String id;
public String name;
public int age;
public Person(String id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
}

程序有异常,还有判断文件是否存在我也没处理,没运行,请自行尝试。。。

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答