zoukankan      html  css  js  c++  java
  • 文件的横纵转换

    通过文件的横纵转换对java的相关基础知识进行巩固

    应用到的知识点有:

    1.流(在此过程中主要用到的BufferedReader和BufferedWriter)

    2.集合

    重点突破:

    对于这个问题重点就是一些思想的转化

    1.如何拿到student.txt文件中的第一行

    2.在第一行拿到之后,如何和后面的数据对应起来

    需求:将下面的格式进行转换

    student.txt

     转换后的格式:

    student1.txt

     具体代码如下所示:

    package com.gcy;

    import java.io.Serializable;

    public class Person implements Serializable,Comparable<Person>{
    private String stuNo;
    private String name;
    private String gender;
    private String age;
    private String height;
    private String weight;
    private String tel;

    public Person() {
    super();
    }

    public Person(String stuNo, String name, String gender, String age, String height, String weight, String tel) {
    super();
    this.stuNo = stuNo;
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.height = height;
    this.weight = weight;
    this.tel = 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 getHeight() {
    return height;
    }

    public void setHeight(String height) {
    this.height = height;
    }

    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 "Person [stuNo=" + stuNo + ", name=" + name + ", gender=" + gender + ", age=" + age + ", height="
    + height + ", weight=" + weight + ", tel=" + tel + "] ";
    }
    /**
    * 根据学好进行排序
    * @param o
    * @return
    */
    @Override
    public int compareTo(Person o) {
    return this.stuNo.compareTo(o.stuNo);
    }

    }

    ==========================================================

    package com.gcy;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;

    public class Test {

    public static void main(String[] args) throws Exception {
    convertor();
    System.out.println("转换成功了");
    }

    public static void convertor() throws Exception {
    // 创建一个高效的字符缓冲流,进行的读取
    BufferedReader br = new BufferedReader(new FileReader("student.txt"));
    String line = null;
    // 创建一个集合,对person进行保存
    List<Person> pList = new ArrayList<Person>();
    // 判断是否是第一行的标志
    boolean isFirst = true;
    while ((line = br.readLine()) != null) {
    // 获取第一行数据,思想:设置标志,通过标志进行判断
    if (isFirst) {
    // 对字符串进行一个分割,会生成一个字符串数组
    String[] stuNos = line.split(" ");
    // 学好 27s1 36 151s1 26s2 13s3
    // 对第一行数据进行遍历,注意在遍历时应该从数组的第二个开始,第一个为头部信息
    for (int i = 1; i < stuNos.length; i++) {
    // System.out.println(stuNos[i].toString());
    // 创建Person对象
    Person p = new Person();
    p.setStuNo(stuNos[i]);
    pList.add(p);
    }
    // System.out.println(pList);
    isFirst = false;

    } else {
    // 对剩余的所有行进行一个处理

    // * 思想:利用第一行的处理,对集合进行一个遍历

    String[] stuInfos = line.split(" ");
    for (int i = 0; i < pList.size(); i++) {
    // 获取每一个创建好的人
    Person person = pList.get(i);
    if (stuInfos[0].equals("姓名")) {
    person.setName(stuInfos[i + 1]);
    }
    if (stuInfos[0].equals("性别")) {
    person.setGender(stuInfos[i + 1]);
    }
    if (stuInfos[0].equals("年龄")) {
    person.setAge(stuInfos[i + 1]);
    }
    if (stuInfos[0].equals("身高")) {
    person.setHeight(stuInfos[i + 1]);
    }
    if (stuInfos[0].equals("体重")) {
    person.setWeight(stuInfos[i + 1]);
    }
    if (stuInfos[0].equals("电话")) {
    person.setTel(stuInfos[i + 1]);
    }
    }
    }
    }
    Collections.sort(pList);
    BufferedWriter bw=new BufferedWriter(new FileWriter("student1.txt"));
    bw.write("学号 姓名 性别 年龄 身高 体重 电话");
    //当写完一行是一定要记得换行
    bw.newLine();
    //进行一个循环
    for(Person p:pList) {
    bw.write(p.getStuNo()+" "+p.getName()+" "+p.getGender()+" "+p.getAge()+" "+p.getHeight()+" "+p.getWeight()+" "+p.getTel());
    bw.newLine();
    }
    bw.flush();
    }
    }

  • 相关阅读:
    ubuntu重新安装mysql
    linux基本命令
    ubuntu启用root用户
    cada的常规使用
    如果有人对我的mysql的笔记感兴趣请联系我,互相学习
    10、mysql查看进程
    09、Mysql 查询是否锁表
    08、查看锁记录等待时间:
    针对发送网络附件的java方法(使用Apache的jar包调用)
    mysql的卸载重装+导入大量数据失败的解决方案+工具执行和项目执行结果不同
  • 原文地址:https://www.cnblogs.com/juddy/p/12001515.html
Copyright © 2011-2022 走看看