天天看點

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習題解析