2019-08-14 将信息写入文件中2

package com.foreknow.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 要求:从控制台输入5次学生的信息,然后将这些信息写入到一个文件中(student.txt)
 * 数据的格式要求: 1000 tom1 20 18640297654 
 * 1001 tom2 20 18640297654 
 * 1002 tom3 20 18640297654 
 * 1003 tom4 20 18640297654 
 * 1004 tom5 20 18640297654 
 * 要将student.txt文件中的数据读取出来 
 * 分析: 
 * 1.功能:
 * 向文件中写数据 从文件中读取数据 
 * 2.几个类:测试类 学生类 功能类 
 * 使用知识:I/O 使用集合
 * 
 * @author Administrator
 *
 */
public class TestFile {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            System.out.println("请输入学生的编号");
            int id = input.nextInt();
            System.out.println("请输入学生的姓名");
            String name = input.next();
            System.out.println("请输入学生的年龄");
            int age = input.nextInt();
            System.out.println("请输入学生的电话号");
            String phone = input.next();
            Student s = new Student(id, name, age, phone);
//          s.setId(id);
//          s.setName(name);
//          s.setAge(age);
//          s.setPhone(phone);
            // 将以上员工的信息添加到list集合中
            list.add(s);
        }
        Function write = new Function();
//      write.write(list, "D:/student.txt");
        List list2 = write.readFile("D:/student.txt");
        for (Student s : list2) {
            System.out.println(s.getId()+"--"+s.getName());
        }
        
        
//      try {
//          FileReader fr = new FileReader("D:/student.txt");
//          write.read(fr);
//      } catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
        
    }
}

package com.foreknow.io;

public class Student {
    private int id;
    private String name;
    private int age;
    private String phone;
    public Student(int id, String name, int age, String phone){
        this.id = id;
        this.name = name;
        this.age = age;
        this.phone = phone;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

}

package com.foreknow.io;

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

public class Function {
    // 向文件当中写数据
    public void write(List list, String fileName) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            // 1.遍历list集合,获取到对象Student
            Iterator it = list.iterator();
            while (it.hasNext()) {
                Student s = it.next();
                // 2.获取Student对象信息(sid+" "+name+" "+age+" "+phone)
                String str = s.getId() + " " + s.getName() + " " + s.getAge() + " " + s.getPhone();
                // 3.将这个字符串通过I/O流写到文件中(BufferedWriter)
                bw.write(str);
                bw.newLine();
            }
            // 4.关闭资源
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // finally {
    // //4.关闭资源
    // try {
    // bw.close();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // try {
    // fw.close();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }

    //读取文件中的数据
    public List readFile(String fileName){
        FileReader fr = null;//放大作用域
        BufferedReader br = null;
        List list = new ArrayList();
        try {
             fr = new FileReader(fileName);//节点流
             br = new BufferedReader(fr);//处理流
             //读取文件
             String temp;
             while ((temp = br.readLine()) != null) {//1000 tom 30 112121212
                //拆分字符串
                 String[] str = temp.split(" ");
                 //要将拆分出来的字符串放到Student对象中
                 Student s = new Student(0, temp, 0, temp);
                 s.setId(Integer.parseInt(str[0]));
                 s.setName(str[1]);
                 s.setAge(Integer.parseInt(str[2]));
                 s.setPhone(str[3]);
                 //要将Student对象保存到集合list中
                 list.add(s);
            }
             fr.close();
             br.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    
        
//  public void read(FileReader fr) {
//      BufferedReader br = new BufferedReader(fr);
//      String read;
//      try {
//          read = br.readLine();
//          while (read != null) {
//              System.out.println(read);
//              read = br.readLine();
//          }
//          br.close();
//      }
//      catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      } catch (IOException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }

//  }
}

你可能感兴趣的:(2019-08-14 将信息写入文件中2)