本系列博客汇总在这里:Java系列_汇总
一、抽象建立 Student 类
二、把文件解析成学生集合(关键点)
获得每一行,然后按照空格切分。
三、把学生集合写入文件
- 示例源码
public class Student implements Comparable<Student> { /** * 学号 */ private String stuNo; /** * 姓名 */ private String name; /** * 性别 */ private String gender; /** * 年龄 */ private String age; /** * 身高 */ private String tall; /** * 体重 */ private String weight; /** * 电话 */ private String tel; public String getStuNo() { return stuNo; } public void setStuNo(String stuNo) { this.stuNo = stuNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getTall() { return tall; } public void setTall(String tall) { this.tall = tall; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } @Override public String toString() { return "\n"+"Student [stuNo=" + stuNo + ", name=" + name + ", gender=" + gender + ", age=" + age + ", tall=" + tall + ", weight=" + weight + ", tel=" + tel + "]"; } @Override public int compareTo(Student o) { return this.stuNo.compareTo(o.stuNo); } }
public class fileUtils { public static void main(String[] args) { parseFile(); writeFile(); } /** * 定义存储学生对象的集合 */ static List<Student> sList = new ArrayList<Student>(); /** * 解析文件 */ public static void parseFile() { // 定义字符缓冲区 BufferedReader br = null; try { br = new BufferedReader(new FileReader("Student.txt")); boolean isFirstRow = true; // 定义存储行变量 String line = null; while((line = br.readLine()) != null) { // 把每一行通过空格来切分 String[] arr = line.split(" "); // 如果是第一行 if(isFirstRow) { // 遍历第一行的学号,使用普通for循环 for(int i = 1; i < arr.length; i++) { // 创建学生对象 Student s = new Student(); // 把学生加入集合 sList.add(s); } isFirstRow = false; } setAttr(arr); } } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }finally { try { if(br != null) { br.close(); } } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } // 给学生复制属性 public static void setAttr(String[] arr) { // 获得每一行的头 String head = arr[0]; // 循环行的每一个拆分项,从第二项开始 for(int i = 1; i < arr.length; i++) { // 根据集合中的索引来获得学生对象 Student s = sList.get(i-1); switch(head) { case "学号": s.setStuNo(arr[i]); break; case "姓名": s.setName(arr[i]); break; case "性别": s.setGender(arr[i]); break; case "年龄": s.setAge(arr[i]); break; case "身高": s.setTall(arr[i]); break; case "体重": s.setWeight(arr[i]); break; case "电话": s.setTel(arr[i]); break; default: break; } } } /** * 把集合写入文件 */ public static void writeFile() { // 排序 Collections.sort(sList); PrintWriter pw = null; try { pw = new PrintWriter("StudentResult.txt"); // 写头 pw.println("学号\t\t姓名\t\t性别\t\t年龄\t\t身高\t\t体重\t\t电话"); for(Student s : sList) { pw.println(s.getStuNo()+"\t"+s.getName()+"\t\t"+s.getGender()+"\t\t"+s.getAge()+"\t\t"+s.getTall()+"\t"+s.getWeight()+"\t"+s.getTel()); pw.flush(); } } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }finally { if(pw != null) { pw.close(); } } } }
如有错误,欢迎指正!