Vertical and horizontal conversion file

Consolidation through basic knowledge of vertical and horizontal convert the file to the java

Apply the knowledge points are:

1. Flow (used in the process of primary and BufferedWriter, BufferedReader)

2. collection

Breakthroughs in key areas:

For this problem is to focus on some of the ideas of transformation

1. How to get the first line of the file student.txt

2. After the first row to get, and how to back up the data corresponding

Requirements: The following format conversion

student.txt

 

 After the conversion format:

 

student1.txt

 

 Specific code as follows:

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 + "]\n";
}
/**
* 根据学好进行排序
* @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 {

static void main public (String [] args) throws Exception {
Convertor ();
System.out.println ( "conversion successful");
}

static void Convertor public () throws Exception {
// create a character buffer efficient flow, Reading and
the BufferedReader the BufferedReader br = new new (new new the FileReader ( "student.txt"));
String Line = null;
// Create a set , to save the person
List <the person> = new new plist the ArrayList <the person> ();
// determines whether the first row flag
Boolean = isFirst to true;
the while (! (= br.readLine line ()) = null) {
// get the first line of data, ideas: setting a flag, a flag is determined by
IF (isFirst) {
// for dividing a string, it generates a string array
string [] stuNos = line.split ( " ");
36 // learn 151s1 26S2 13s3 27s1
// data traversing the first row, note that when traversing should start from the second array, first one of the header information
for (int i = 1; i <stuNos.length ; I ++) {
// System.out.println (stuNos [I] .toString ());
// Create a Person object
Person p = new Person ();
p.setStuNo(stuNos[i]);
pList.add(p);
}
// System.out.println(pList);
isFirst = false;

The else {}
// for all remaining rows is a process

// * idea: using the first line of treatment, to traverse a collection

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 new BufferedWriter (new new FileWriter ( "student1.txt"));
bw.write ( "Student ID Name Gender Age Height Weight telephone");
// When the finish line is sure to remember wrap
bw.newLine ();
// one cycle
for (the Person P: plist) {
bw.write (p.getStuNo () + "" + p.getName () + "" + p.getGender () + "" p.getAge + () + "" + p.getHeight () + "" + p.getWeight () + "" + p.getTel ());
bw.newLine ();
}
bw.flush ();
}
}

Guess you like

Origin www.cnblogs.com/juddy/p/12001515.html