来实现。

import java.io.*;
import java.util.*;

public class AddNamesToFile {
    public static void main(String[] args) {
        Set<String> names = new HashSet<>(Arrays.asList("Alice", "Bob", "Charlie", "David", "Emma"));

        try (BufferedWriter writer = new BufferedWriter(new FileWriter("name.txt"))) {
            for (String name : names) {
                writer.write(name);
                writer.newLine();
            }
            System.out.println("Names added to file successfully.");
        } catch (IOException e) {
            System.out.println("Error writing to file: " + e.getMessage());
        }
    }
}
``
添加5个学生的姓名到nametxt文件中使用集合的方式用字符缓冲输入输出流利用java语言

原文地址: https://www.cveoy.top/t/topic/fq3c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录