代码编织梦想

package com.rick;
 
public class Student {
	private String id;
	private String name;
	private String age;
	private String address;
	public Student() {
		super();
	}
	public Student(String id, String name, String age, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
	}
	
 
}

package com.rick;
 
import java.io.*;
import java.util.*;
 
public class ArrayListToFile {
 
	public static void main(String[] args) throws IOException {
		//创建学生集合
		ArrayList<Student> array = new ArrayList<Student>();
		Scanner sc = new Scanner(System.in);
		//循环添加学生
		System.out.println("添加几个学生?");
		int num = sc.nextInt();
		for (int i = 0; i < num; i++) {
			addStudent(array);
		}
		//创建输出缓冲流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
		//循环输出到文件
		for (Student student : array) {
			System.out.println(student.toString());
			bw.write(student.getId()+","+student.getName()+","+student.getAge()+","+student.getAddress());
			bw.newLine();
		}
		bw.close();
		
	}
 
	public static void addStudent(ArrayList<Student> array) {
		Scanner sc = new Scanner(System.in);
		String id;
		// 判断学号是否存在
		while (true) {
			System.out.println("请输入学生学号:");
			id = sc.nextLine();
			boolean flag = false;
			for (int i = 0; i < array.size(); i++) {
				Student s = array.get(i);
				if (s.getId().equals(id)) {
					flag = true;
					break;
				}
			}
			if (flag == true) {
				System.out.println("该学生已存在,请重新输入:");
			} else {
				break;
			}
		}
		System.out.println("请输入学生姓名:");
		String name = sc.nextLine();
		System.out.println("请输入学生年龄:");
		String age = sc.nextLine();
		System.out.println("请输入学生住址:");
		String address = sc.nextLine();
		Student s = new Student();
		s.setId(id);
		s.setName(name);
		s.setAge(age);
		s.setAddress(address);
 
		//添加到集合
		array.add(s);
		System.out.println("添加成功!");
	}
 
}
 
package com.rick;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
 
public class FileToArrayList {
	public static void main(String[] args) throws IOException {
		//创建一个输入缓冲对象
		BufferedReader br = new BufferedReader(new FileReader("student.txt"));
		//创建集合对象
		ArrayList<Student> array = new ArrayList<Student>();
		//读取文件
		String line;
		while((line = br.readLine()) != null){//br.read.line读一行
			String[] strs = line.split(",");//分割
			Student s = new Student();
			s.setId(strs[0]);
			s.setName(strs[1]);
			s.setAge(strs[2]);
			s.setAddress(strs[3]);
			array.add(s);		
		}
		//释放资源
		br.close();
		//打印到控制台
		for (Student student : array) {
			System.out.println("学号:"+student.getId()+"  姓名:"+student.getName()
			+"  年龄:"+student.getAge()+"  地址:"+student.getAddress());
		}
	}
}`

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/qq_52000019/article/details/111093310