Java学习之IO流四
1.用代码实现以下需求
(1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
(2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
(3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
(4)读取D:\\StudentInfo.txt文件中的ArrayList对象
(5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
(6)将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)
1 /** 2 * (1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性, 3 * 生成空参有参构造,set和get方法,toString方法 4 * @author vanguard 5 * @param <T> 6 * 7 */ 8 public class Student implements Serializable { 9 /** 10 * 11 */ 12 private static final long serialVersionUID = 1234545434L; 13 private String name; 14 private String gender; 15 private int age; 16 17 public Student() {} 18 19 public Student(String name, String gender, int age) { 20 super(); 21 this.name = name; 22 this.gender = gender; 23 this.age = age; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getGender() { 35 return gender; 36 } 37 38 public void setGender(String gender) { 39 this.gender = gender; 40 } 41 42 public int getAge() { 43 return age; 44 } 45 46 public void setAge(int age) { 47 this.age = age; 48 } 49 50 @Override 51 public int hashCode() { 52 final int prime = 31; 53 int result = 1; 54 result = prime * result + age; 55 result = prime * result + ((gender == null) ? 0 : gender.hashCode()); 56 result = prime * result + ((name == null) ? 0 : name.hashCode()); 57 return result; 58 } 59 60 @Override 61 public boolean equals(Object obj) { 62 if (this == obj) 63 return true; 64 if (obj == null) 65 return false; 66 if (getClass() != obj.getClass()) 67 return false; 68 Student other = (Student) obj; 69 if (age != other.age) 70 return false; 71 if (gender == null) { 72 if (other.gender != null) 73 return false; 74 } else if (!gender.equals(other.gender)) 75 return false; 76 if (name == null) { 77 if (other.name != null) 78 return false; 79 } else if (!name.equals(other.name)) 80 return false; 81 return true; 82 } 83 84 @Override 85 public String toString() { 86 return "Person [name=" + name + ", gender=" + gender + ", age=" + age 87 + "]"; 88 } 89 90 91 }
1 /** 2 * 1.用代码实现以下需求 3 (1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性, 4 生成空参有参构造,set和get方法,toString方法 5 (2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息, 6 将6个学员信息存入到ArrayList集合中 7 (3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中 8 (4)读取D:\\StudentInfo.txt文件中的ArrayList对象 9 (5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序 10 (6)将ArrayList集合中排序后的结果利用PrintWriter流写入到 11 E:\\StudentInfo.txt文件中(写入格式:张三-男-25) 12 * @author vanguard 13 * 14 */ 15 public class Demo01 { 16 public static void main(String[] args) throws IOException, ClassNotFoundException { 17 List<Student> list = new ArrayList<Student>(); 18 // list = addStudentInfo(list); 19 // writeStudentInfo(list); 20 list = readStudentInfo(list); 21 list = sortStudent(list); 22 writeStudentInfoToFile(list); 23 24 } 25 /** 26 * 将ArrayList集合中排序后的结果利用PrintWriter流写入到 27 E:\\StudentInfo.txt文件中(写入格式:张三-男-25) 28 * @param list 29 * @throws IOException 30 */ 31 private static void writeStudentInfoToFile(List<Student> list) throws IOException { 32 PrintWriter pw = new PrintWriter(new File("StudentInfo.txt")); 33 for(Student s : list) { 34 pw.println(s.getName() + "-" + s.getGender() + "-" + s.getAge()); 35 pw.flush(); 36 } 37 pw.close(); 38 } 39 /** 40 * 对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序 41 * @param list 42 * @return 43 */ 44 private static List<Student> sortStudent(List<Student> list) { 45 //6个学生对象进行去重 46 Set<Student> set = new HashSet<Student>(); 47 List<Student> stu = new ArrayList<Student>(); 48 set.addAll(list); 49 //按照年龄从小到大的顺序排序 50 TreeSet<Student> students = new TreeSet<Student>(new Comparator<Student>() { 51 52 @Override 53 public int compare(Student o1, Student o2) { 54 55 return o1.getAge() < o2.getAge() ? -1 : 1; 56 } 57 58 }); 59 for(Student s : set) { 60 students.add(s); 61 } 62 for(Student s : students) { 63 stu.add(s); 64 } 65 return stu; 66 } 67 68 /** 69 * 读取D:\\StudentInfo.txt文件中的ArrayList对象 70 * 对象的反序列化 71 * @param list 72 * @return 73 * @throws IOException 74 * @throws ClassNotFoundException 75 */ 76 private static List<Student> readStudentInfo(List<Student> list) throws IOException, ClassNotFoundException { 77 FileInputStream fis = new FileInputStream(new File("student.txt")); 78 ObjectInputStream ois = new ObjectInputStream(fis); 79 list = (List<Student>) ois.readObject(); 80 for(Student s : list) { 81 System.out.println(s); 82 } 83 return list; 84 } 85 /** 86 * 将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中 87 * 对象的序列化 88 * @param list 89 * @throws IOException 90 */ 91 private static void writeStudentInfo(List<Student> list) throws IOException { 92 FileOutputStream fos = new FileOutputStream(new File("student.txt")); 93 ObjectOutputStream os = new ObjectOutputStream(fos); 94 os.writeObject(list); 95 os.close(); 96 fos.close(); 97 } 98 /** 99 * 将学生信息添加到List集合中 100 * 键盘录入6个学生信息 101 * @param list 102 * @return 103 */ 104 private static List<Student> addStudentInfo(List<Student> list) { 105 //键盘录入6个学员信息 106 Scanner sc = new Scanner(System.in); 107 for(int i = 0; i < 6; i++) { 108 String personInfo = sc.next(); 109 String[] s = personInfo.split(","); 110 Student student = new Student(s[0], s[1], Integer.parseInt(s[s.length-1])); 111 list.add(student); 112 } 113 return list; 114 } 115 }
2.用代码实现以下需求:
(1)已知配置文件config.properties中有三个键值对
name=zhangsan
score=80
address=beijing
(2)使用IO字节流对象和Properties类结合使用,将配置文件中的score键的值修改为100
1 /** 2 * 2.用代码实现以下需求: 3 (1)已知配置文件config.properties中有三个键值对 4 name=zhangsan 5 score=80 6 address=beijing 7 (2)使用IO字节流对象和Properties类结合使用,将配置文件中的score键的值修改为100 8 * @author vanguard 9 * 10 */ 11 public class Demo02 { 12 public static void main(String[] args) throws IOException { 13 Properties prop = new Properties(); 14 //创建字符输入流对象 15 FileReader fr = new FileReader("config.properties"); 16 //流对象读取文件中的键值对,保存到集合 17 prop.load(fr); 18 //修改键为score的值为100 19 prop.setProperty("score", "100"); 20 //创建字符输出流对象 21 FileWriter fw = new FileWriter("config.properties"); 22 //接收所有的字符的输出流,将集合中的键值对,写回文件中保存 23 prop.store(fw, ""); 24 //释放资源 25 fw.close(); 26 fr.close(); 27 } 28 }
3.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录
1 /** 2 * 3.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录 3 * @author vanguard 4 * 5 */ 6 public class Demo03 { 7 public static void main(String[] args) { 8 File src = new File("d:\\TortoiseSVN"); 9 File dest = new File("d:\\txt"); 10 //定义文件个数 11 int count = 0; 12 try { 13 count = fileNum(src, dest, count); 14 } catch (IOException e) { 15 16 e.printStackTrace(); 17 } 18 System.out.println("txt文件的个数为:" + count); 19 } 20 21 /** 22 * 统计指定目录及子目录下所有txt文件的个数 23 * @param src 24 * @throws IOException 25 */ 26 private static int fileNum(File src, File dest, int count) throws IOException { 27 //遍历目录下的文件及目录 28 for(File f : src.listFiles()) { 29 if(f.isDirectory()) { 30 count = fileNum(f, dest, count); 31 } else { 32 //如果是文件,且以.txt结尾,文件个数加1,调用复制文件方法 33 if(f.getName().endsWith(".txt")) { 34 count++; 35 copyFile(f, new File(dest, f.getName())); 36 } 37 } 38 } 39 return count; 40 } 41 42 /** 43 * 复制文件 44 * @param src 45 * @param dest 46 * @throws IOException 47 */ 48 private static void copyFile(File src, File dest) throws IOException { 49 //定义字符输入流缓冲流对象 50 BufferedReader br = new BufferedReader(new FileReader(src)); 51 //打印流对象 52 PrintWriter pw = new PrintWriter(new FileWriter(dest)); 53 //定义读取字符串 54 String line = null; 55 while(null != (line = br.readLine())) { 56 pw.println(line); 57 58 } 59 //释放资源 60 br.close(); 61 pw.close(); 62 } 63 }