Java将集合(List,Set)中的字符串数据写入到文本文件

练习一:
需求:
把ArrayList集合中的字符串数据写入到文本文件。要求:每一个字符串元素作为文件中的一行数据

思路:
    1:创建ArrayList集合
    2:往集合中存储字符串元素
    3:创建字符缓冲输出流对象
    4:遍历集合,得到每一个字符串数据
    5:调用字符缓冲输出流对象的方法写数据
    6:释放资源
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
    需求:
        把ArrayList集合中的字符串数据写入到文本文件。要求:每一个字符串元素作为文件中的一行数据

    思路:
        1:创建ArrayList集合
        2:往集合中存储字符串元素
        3:创建字符缓冲输出流对象
        4:遍历集合,得到每一个字符串数据
        5:调用字符缓冲输出流对象的方法写数据
        6:释放资源
 */
public class ArrayListToTxtDemo {
    public static void main(String[] args) throws IOException {
        //创建ArrayList集合
        ArrayList<String> array = new ArrayList<String>();

        //往集合中存储字符串元素
        array.add("hello");
        array.add("world");
        array.add("java");

        //创建字符缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("32myCharStream\\array.txt"));

        //遍历集合,得到每一个字符串数据
        for(String s : array) {
            //调用字符缓冲输出流对象的方法写数据
            bw.write(s);
            bw.newLine();
            bw.flush();
        }

        //释放资源
        bw.close();
    }
}

练习二:
需求:
把ArrayList集合中的学生数据写入到文本文件。要求:每一个学生对象的数据作为文件中的一行数据
格式:学号,姓名,年龄,居住地 举例:001,林青霞,30,西安

思路:
    1:定义学生类
    2:创建ArrayList集合
    3:创建学生对象
    4:把学生对象添加到集合中
    5:创建字符缓冲输出流对象
    6:遍历集合,得到每一个学生对象
    7:把学生对象的数据拼接成指定格式的字符串
    8:调用字符缓冲输出流对象的方法写数据
    9:释放资源

学生类:

public class Student {
    private String sid;
    private String name;
    private int age;
    private String address;

    public Student() {
    }

    public Student(String sid, String name, int age, String address) {
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    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 getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

实现类:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
    需求:
        把ArrayList集合中的学生数据写入到文本文件。要求:每一个学生对象的数据作为文件中的一行数据
        格式:学号,姓名,年龄,居住地	举例:001,林青霞,30,西安

    思路:
        1:定义学生类
        2:创建ArrayList集合
        3:创建学生对象
        4:把学生对象添加到集合中
        5:创建字符缓冲输出流对象
        6:遍历集合,得到每一个学生对象
        7:把学生对象的数据拼接成指定格式的字符串
        8:调用字符缓冲输出流对象的方法写数据
        9:释放资源
 */
public class ArrayListToFileDemo {
    public static void main(String[] args) throws IOException {
        //创建ArrayList集合
        ArrayList<Student> array = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("001", "林青霞", 30, "西安");
        Student s2 = new Student("002", "张曼玉", 35, "武汉");
        Student s3 = new Student("003", "王祖贤", 33, "郑州");

        //把学生对象添加到集合中
        array.add(s1);
        array.add(s2);
        array.add(s3);

        //创建字符缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("32myCharStream\\students.txt"));

        //遍历集合,得到每一个学生对象
        for (Student s : array) {
            //把学生对象的数据拼接成指定格式的字符串
            StringBuilder sb = new StringBuilder();
            sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());

            //调用字符缓冲输出流对象的方法写数据
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }

        //释放资源
        bw.close();
    }
}

练习3:
需求:键盘输入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照要求照成绩总分从高到低写入文件
格式:姓名,语文成绩,数学成绩,英语成绩 举例:凌青霞,98,99,100

    思路:
        1:定义学生类
        2:创建TreeSet集合,通过比较器进行排序
        3:键盘录入学生数据
        4:创建学生对象,吧键盘录入的数据对应赋值给学生对象的成员变量
        5:把学生对象添加到TreeSet集合
        6:创建字符缓存输出流对象
        7:遍历集合,得到每一个学生对象
        8:把学生对象拼接成指定格式的字符串
        9:调用字符缓冲输出流对象的方法写数据
        10:释放资源

学生类:

public class Student {
    private String name;
    private int chinese;
    private int math;
    private int english;

    public Student (){
        super();
    }

    public Student(String name, int chinese, int math, int english) {
        super();
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }
    public int getSum(){
        return this.chinese+this.math+this.english;
    }

}

实现类:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class TreeSetToFileDemo {
    public static void main(String[] args) throws IOException {
        //2:创建TreeSet集合,通过比较器进行排序
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //成绩总分从高到低
                int num = s2.getSum() - s1.getSum();
                //次要条件
                int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
                int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
                int num4 = num3 == 0 ? s1.getName().compareTo(s2.getName()) : num3;

                return num4;
            }
        });
        //            3:键盘录入学生数据
        for (int i = 0; i < 5; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请录入低"+(i+1)+"个学生信息");
            System.out.print("姓名:");
            String name = sc.nextLine();
            System.out.print("语文成绩:");
            int chinese = sc.nextInt();
            System.out.print("数学成绩:");
            int math = sc.nextInt();
            System.out.print("英语成绩:");
            int english = sc.nextInt();
            //4:创建学生对象,吧键盘录入的数据对应赋值给学生对象的成员变量
            Student s = new Student();
            s.setName(name);
            s.setChinese(chinese);
            s.setMath(math);
            s.setEnglish(english);
            //            5:把学生对象添加到TreeSet集合
            ts.add(s);

        }

        //  6:创建字符缓存输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("32myCharStream\\ts.txt"));
        // 7:遍历集合,得到每一个学生对象
        for(Student s:ts){
            // 8:把学生对象拼接成指定格式的字符串
            StringBuilder sb = new StringBuilder();
            sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());
            // 9:调用字符缓冲输出流对象的方法写数据
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }



        //            10:释放资源
        bw.close();
    }

发布了37 篇原创文章 · 获赞 0 · 访问量 321

猜你喜欢

转载自blog.csdn.net/qq_38454176/article/details/104017434