本程序利用List集合实现Student对象的存储与管理。请在阅读程序基础上完成填空(共5个空)。import java.util.*;class Student{private int id;private String name;private int age;public Student(int id,String name,int age){this.id=id;this.name=name;this.age=age;}// 此处为各属性的setter和getter方法(略)...public String toString(){return name + ":" + id + ":" + age;// 注:字符串中的冒号为英文符号}}public class TestList {public static void main(String[] args) {//声明List对象,创建LinkedList对象List list = (1);Student stu1 = new Student(20001, "蔡长锦", 20);Student stu2 = new Student(20006, "皮慧芳", 21);Student stu3 = new Student(20003, "董凯莉", 19);Student stu4 = new Student(20008, "孟卫东", 26);Student stu5 = new Student(20009, "齐贵田", 22);list.add(stu1);list.add(stu2);list.add(stu3);list.add(stu4);list.add(stu5);// 使用普通for循环遍历所有的Student对象for (int i = 0; i < list.size(); i++) {System.out.println((2) );}// 下面的语句删除学号为20006的学生(3) ;// 使用迭代器遍历所有的Student对象Iterator iter = (4) ;while((5) ){System.out.println(iter.next());}}}
本程序利用字符流实现文本文件的复制,请在阅读程序的基础上完成填空(共5个空)。import java.io.*;public class TestTextFileCopy {public static void main(String[] args) throws IOException {String source_path = "d:/aboutnuc.txt";String destination_path = "d:/aboutnuc_copy";(1).copy(source_path, destination_path);}}class TextFileCopy{// 用字符流完成文本文件的复制public void copy(String source_path, String destination_path) throws IOException {// 声明并创建长度为10的字符数组char[] charBuf = (2) ;FileReader fr = (3);FileWriter fw = new FileWriter(destination_path);int n;while ((n = (4) ) != (5) ) {fw.write(charBuf, 0, n);}System.out.println();// 此处为关闭流对象的代码(略)...}}
下面的程序利用缓冲流读写用户信息文件(users.txt),请在阅读程序的基础上完成填空(共5个空)。import java.io.*;import java.util.*;class User {private String id;private String type;private String password;private String name;private byte sex;private String city;public User(String id, String type, String password, String name, byte sex, String city) {super();this.id = id;this.type = type;this.password = password;this.name = name;this.sex = sex;this.city = city;}public User(String[] userInfo){this.id = userInfo[0];this.type = userInfo[1];this.password = userInfo[2];this.name = userInfo[3];this.sex = Byte.parseByte(userInfo[4]);this.city = userInfo[5];}// 此处为各属性的setter和getter方法(略)...@Overridepublic String toString() {return id + ":" + type + ":" + password + ":" + name + ":" + sex + ":" + city;}}public class TestIOForUserFile {public static void main(String[] args) {Map map = new HashMap();map.put("u00001", new User("u00001", "普通用户", "pw00001", "甄剑峰", (byte)1, "太原"));map.put("u00004", new User("u00004", "管理员", "pw00002", "赫梅丽", (byte)0, "北京"));map.put("u00006", new User("u00006", "普通用户", "pw00003", "杨宇奇", (byte)1, "上海"));map.put("u00010", new User("u00010", "普通用户", "pw00010", "安鹏洲", (byte)1, "深圳"));Iterator iterValue = map.values().iterator();String fileName = "users.txt";FileWriter fw = null;BufferedWriter bw = null;try {fw = new FileWriter(fileName, true);bw = new BufferedWriter(fw);while (iterValue.hasNext()) {User user = iterValue.next();// 下面的语句将user的信息(可以通过user.toString()方法获取得到)写入缓冲输出流bw(1);bw.newLine();}} catch (IOException e) {e.printStackTrace();} finally {// 此处为关闭流对象代码(略)}Map map2 = new HashMap();FileReader fr = null;BufferedReader br = null;String[] userInfo;User user2;String s;try {fr = new FileReader(fileName);br = (2);while((s = (3) ) != null){userInfo = s.split(":");user2 = new User(userInfo);// 将user2以键值对形式添加map2中map2.put((4) , user2);}Iterator> iterEntry = map.entrySet().iterator();while(iterEntry.hasNext()){Map.Entry entry = (Map.Entry)iterEntry.next();// 按key <=> value形式输出键值映射对System.out.println((5) + "<=>" + entry.getValue());}} catch (IOException e) {e.printStackTrace();} finally {// 此处为关闭流对象代码(略)}}}