/*
字节流结合类,实现简单录入信息,并在本地存储
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws IOException {
ArrayList<Student> list = new ArrayList<>();
//创建"学生信息"文件夹
File file = new File("F:\\学生信息");
file.mkdirs();
//键盘录入学生信息
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
//将学生信息添加到集合
list.add(new Student(name, age));
}
//遍历集合得到每个学生的基础信息
for (Student student : list) {
String name = student.getName();
String age = Integer.toString(student.getAge());
//创建以学生姓名命名的文本
String s = name + "--" + age;
File file1 = new File(file, name + ".txt");
boolean newFile = file1.createNewFile();
System.out.println(name + "信息创建" + newFile);
//写入学生信息到对应TXT文本
FileOutputStream fos = new FileOutputStream(file1);
fos.write(s.getBytes());
fos.close();
}
}
}
基本学生类![在这里插入图片描述](https://img-blog.csdnimg.cn/20201214222604766.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0l2ZW53Zg==,size_16,color_FFFFFF,t_70#pic_center)
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
![```](https://img-blog.csdnimg.cn/20201214222720290.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0l2ZW53Zg==,size_16,color_FFFFFF,t_70#pic_center)
字节流结合类,实现简单录入信息,并在本地存储
最新推荐文章于 2024-04-12 10:53:40 发布