发现IO的强大,是引用jar包Apache,里面的api比java自带的api好用(意思就是省事,可以偷懒)
今天在论坛里,有个人问了怎么讲输入的写入在文件里,哎,试着装逼的心态答了一答,果然,程序这东西你知道了不熟能生巧,还是不行的啊!下面做法略为粗糙。
public static void main(String[] args) throws IOException {
Student[] stu = new Student[5];Scanner s = new Scanner(System.in);
for(int i=0;i<1;i++)
{
stu[i] = new Student();
System.out.printf("请输入第%d名学生姓名: ", i+1);
stu[i].setSname(s.nextLine());
System.out.printf("请输入第%d名学生学号: ", i+1);
stu[i].setSno(s.nextLong());
System.out.printf("请输入第%d名学生高数成绩: ", i+1);
stu[i].setScore1(s.nextInt());
System.out.printf("请输入第%d名学生物理成绩: ", i+1);
stu[i].setScore2(s.nextInt());
s.nextLine();
}
File aFile = new File("D:/work/stu6.txt"); //在这样的绝对路径下创建txt文件
FileWriter aWriter = new FileWriter(aFile,true);//这里要些true,我没写true的时候,写入为空,写入不了
if(aFile.exists())
{
aFile.delete();
}
for(int i = 0;i<1;i++)
{
StringBuffer aBuffer = new StringBuffer();
String aString = aBuffer.append( stu[i].getSname()).append(Long.toString(stu[i].getSno())).append(Integer.toString(stu[i].getScore1())).append(Integer.toString(stu[i].getScore2())).toString();
System.out.println(aString);
aWriter.write(aString);//写入字符串
aWriter.flush();
aWriter.close();//记住close
}