天天看点

IO习题解析

题目

1.程序启动时,如果文件存在,读取文件,List < Student>

2.程序结束时,将信息写入文件

分析

读文件:

如果文件不存在。文件存在。

zhangsan,23,100 一行====》Student Student String

Student类

package IO_25;

public class Student {
  String name;
  int age;
  int score;
  public String studentToLine(){
    return this.getName()+","+this.getAge()+","+this.getScore();
  }
  public Student() {
    super();
    // TODO Auto-generated constructor stub
  }
  public Student(String name, int age, int score) {
    super();
    this.name = name;
    this.age = age;
    this.score = score;
  }
  //
  public Student(String line){
    //this必须在第一行
    //依据  , 来分隔line,并将数据存储到args数组
    String []args=line.split(",");
    //依次将第一个给到名字
    this.name=args[0];
    //将第两个给到年龄
    this.age=Integer.parseInt(args[1]);
    //,强制转换String类型为int;将第三个给到分数
    this.score=Integer.parseInt(args[2]);
  }
  @Override
  public String toString() {
    return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public int getScore() {
    return score;
  }
  public void setScore(int score) {
    this.score = score;
  }
}      

StudentMangerDemo 类

package IO_25;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StudentMangerDemo {
//定义一个saveStudents函数,实现存储学生信息的功能
  static void saveStudents(List<Student>students){
    //创建一个字符缓冲输出流对象
    BufferedWriter bw=null;
    try {
      bw=new BufferedWriter(new FileWriter("student.dat"));
      for(Student student:students){
        bw.write(student.studentToLine());
        bw.newLine();
      }
      bw.flush();
    } catch (Exception e) {
      
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally {
      try {
        bw.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    
  }
  
  static List<Student> loadStudents(String fileName) {
    //新建ArrayList数组
    List<Student>students=new ArrayList<Student>();
    //创建了一个File对象,名字是newFile
    //然后就可以调用这个对象的相关方法完成文件创建,删除,读取,写入等操作
    File file=new File(fileName);
    if(file.exists()){//如果文件存在
      //建一个缓冲字符输入流。
      BufferedReader br=null;
      try {
        //创建一个名为student.dat的文件
        br=new BufferedReader(new FileReader("student.dat"));
        String line=br.readLine();
        while(line!=null){
          students.add(new Student(line));
          line=br.readLine();
        }

      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }finally {
        try {
          br.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }else {//返回students
      return students;
    }
  
    return students;
  }
  
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<Student>students=new ArrayList<Student>();
    students.add(new Student("zhangdan",23,100));
    //

//    String line="zhangsan,23,100";
//    Student s=new Student(line);
//    System.out.println(s);
    System.out.println(loadStudents("student.dat"));
    saveStudents(students);
    
  }

}      

查找class文件

(1)第一种实现

listFile函数

static void listFile(File file){
    File files[]=file.listFiles();
    for(File f:files){
      if (f.isFile()) {
        if (f.getName().endsWith(".java")) {
          System.out.println(f.getName());
        }
      }else {
        listFile(f);
      }
    }
  }      

主函数

public static void main(String[] args) {
  // TODO Auto-generated method stub
  //列出class文件
  listFile(new File("."));
}      

(2)第二种实现方法

递归方法findFiles()

IO习题解析