/*
 *题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。   
 **/
 package com.cn.testquestion;
 
 import java.io.*;
 public class StudentGrate
 {
  public static void main(String args[])throws Exception
  {
  Student[] student={
  new Student("10001","LiNing",88,99,68),
  new Student("10002","LiLie",80,79,85),
  new Student("10003","YunFei",88,90,67),
  new Student("10004","ChunLi",55,91,73),
  new Student("10005","ZhangSan",96,80,75)
  };
  /*
  Student student1=new Student("10001","LiNing",88,99,68);
  Student student2=new Student("10002","LiLie",80,79,85);
  Student student3=new Student("10003","YunFei",88,90,67);
  Student student4=new Student("10004","ChunLi",55,91,73);
  Student student5=new Student("10005","ZhangSan",96,80,75);
  */
 
  for(int i=0;i<student.length;i++)
  {
  new FileStudent(student[i]).toFileIn();
 
  }
 
  }
 }
 
 class Student
 {
  private String xuehao;
  private String name;
  private int shuxue;
  private int yuwen;
  private int yingyu;
  private int average;
 
  public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
  {
  this.xuehao=xuehao;
  this.name=name;
  this.shuxue=shuxue;
  this.yuwen=yuwen;
  this.yingyu=yingyu;
  this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
  }
 
  public String toString()
  {
  return "学号:"+this.xuehao+" 姓名:"+this.name+" 数学成绩:"+this.shuxue+" 语文成绩:"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+" === ";
  }
 }
 
 class FileStudent
 {
  private static String str="";
  private Student person;
 
  public FileStudent(Object person)
  {
  this.person=(Student)person;
  }
 
  public void toFileIn()throws Exception
  {
  //String str="";
  File file=new File("stud.txt");
  if(!file.exists())
  file.createNewFile();
  //else
  // System.out.println("文件已经存在");
 
  FileWriter fileto=new FileWriter(file);
 
  str=str+person.toString();
  fileto.write(str);
  fileto.close();
  }
 
 }
这个是之前看到的一个题目自己试了下,能够打印到文件,但是不能换行。用过 /n  但没成功,不知道有没有别的办法实现。

解决方案 »

  1.   

    System.getProperty("line.separator") 取系统换行符的方法
      

  2.   

    不知道你是 怎么试的,我测试了可以换行才回复的
    package test;import java.io.*;
     public class Test
     {
         public static void main(String args[])throws Exception
         {
             Student[] student={
                 new Student("10001","LiNing",88,99,68),
                 new Student("10002","LiLie",80,79,85),
                 new Student("10003","YunFei",88,90,67),
                 new Student("10004","ChunLi",55,91,73),
                 new Student("10005","ZhangSan",96,80,75)
             };
             /*
             Student student1=new Student("10001","LiNing",88,99,68);
             Student student2=new Student("10002","LiLie",80,79,85);
             Student student3=new Student("10003","YunFei",88,90,67);
             Student student4=new Student("10004","ChunLi",55,91,73);
             Student student5=new Student("10005","ZhangSan",96,80,75);
             */
             
             for(int i=0;i<student.length;i++)
             {
                 new FileStudent(student[i]).toFileIn();
                 
             }
                 
         }
     }
     
     class Student
     {
         private String xuehao;
         private String name;
         private int shuxue;
         private int yuwen;
         private int yingyu;
         private int average;
         
         public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
         {
             this.xuehao=xuehao;
             this.name=name;
             this.shuxue=shuxue;
             this.yuwen=yuwen;
             this.yingyu=yingyu;
             this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
         }
         
         public String toString()
         {
             return "学号:\n"+this.xuehao+" 姓名:\n"+this.name+" 数学成绩:\n"+this.shuxue+" 语文成绩:\n"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+" === ";
         }
     }
     
     class FileStudent
     {
         private static String str="";
         private Student person;
         
         public FileStudent(Object person)
         {
             this.person=(Student)person;
         }
         
         public void toFileIn()throws Exception
         {
             //String str="";
             File file=new File("stud.txt");
             if(!file.exists())
                 file.createNewFile();
             //else
             //        System.out.println("文件已经存在");
             
             FileWriter fileto=new FileWriter(file);
             
             str=str+person.toString();
             fileto.write(str);
             fileto.close();
         }
         
     }
    这个是文件内容
    学号:
    10001 姓名:
    LiNing 数学成绩:
    88 语文成绩:
    99 英语成绩:68 平均成绩:85 === 学号:
    10002 姓名:
    LiLie 数学成绩:
    80 语文成绩:
    79 英语成绩:85 平均成绩:81 === 学号:
    10003 姓名:
    YunFei 数学成绩:
    88 语文成绩:
    90 英语成绩:67 平均成绩:81 === 学号:
    10004 姓名:
    ChunLi 数学成绩:
    55 语文成绩:
    91 英语成绩:73 平均成绩:73 === 学号:
    10005 姓名:
    ZhangSan 数学成绩:
    96 语文成绩:
    80 英语成绩:75 平均成绩:83 === 而且,貌似换行只有\n吧
      

  3.   

    恢复shaosijun2004
    你说的那个我真的试了,真的不行
      

  4.   

    /*
     *题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。   
     **/
     package com.cn.testquestion;
     
     import java.io.*;
     public class StudentGrate
     {
      public static void main(String args[])throws Exception
      {
      Student[] student={
      new Student("10001","LiNing",88,99,68),
      new Student("10002","LiLie",80,79,85),
      new Student("10003","YunFei",88,90,67),
      new Student("10004","ChunLi",55,91,73),
      new Student("10005","ZhangSan",96,80,75)
      };
      /*
      Student student1=new Student("10001","LiNing",88,99,68);
      Student student2=new Student("10002","LiLie",80,79,85);
      Student student3=new Student("10003","YunFei",88,90,67);
      Student student4=new Student("10004","ChunLi",55,91,73);
      Student student5=new Student("10005","ZhangSan",96,80,75);
      */
     
      for(int i=0;i<student.length;i++)
      {
      new FileStudent(student[i]).toFileIn();
     
      }
     
      }
     }
     
     class Student
     {
      private String xuehao;
      private String name;
      private int shuxue;
      private int yuwen;
      private int yingyu;
      private int average;
     
      public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
      {
      this.xuehao=xuehao;
      this.name=name;
      this.shuxue=shuxue;
      this.yuwen=yuwen;
      this.yingyu=yingyu;
      this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
      }
     
      public String toString()
      {
      String nextline=System.getProperty("line.separator");  return "学号:"+this.xuehao+" 姓名:"+this.name+" 数学成绩:"+this.shuxue+" 语文成绩:"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+nextline;
      }
     }
     
     class FileStudent
     {
      private static String str="";
      private Student person;
     
      public FileStudent(Object person)
      {
      this.person=(Student)person;
      }
     
      public void toFileIn()throws Exception
      {
      //String str="";
      File file=new File("stud.txt");
      if(!file.exists())
      file.createNewFile();
      //else
      // System.out.println("文件已经存在");
     
      FileWriter fileto=new FileWriter(file);
     
      str=str+person.toString();
      fileto.write(str);
      fileto.close();
      }
     
     }
    学到了,谢谢大家。用的是txt文档打开的,能够实现换行了。