2019-08-14 将信息写入文件中(student.txt)

package com.foreknow.io;

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, phone, 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");
    }
}
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.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class Function {
    //向文件当中写数据
    public void write(List list,String fileName){
//      FileWriter fw;
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
//          fw = new FileWriter(fileName);
//          BufferedWriter bw = new BufferedWriter(fw);
            //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();
//              fw.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();
//          }   
//      }
        
        
    }
}

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