您的位置:首页 > 其它

读取txt文件内容,按格式输出

2018-06-07 19:20 141 查看

1.读取txt文件内容
文件内容大致如下:
 00001 张三 计算机系 男…
00002 李四 外语系 女…
读取文件后对内容进行整合,按院系分类输出。格式为:
计算机系
00001 张三 男…
外语系
00002 李四 女…*/

package homework;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class Homework01 {
public static void main(String[] args) {


printGroupInfo(
groupStudentsByDept(convertStringsToStudents(readFileToStringList("src" + File.separator + "1.txt"))));
}


/**
* 打印最终分组结果

* @param map
*/
public static void printGroupInfo(Map<String, List<Student>> map) {
for (Entry<String, List<Student>> ele : map.entrySet()) {
System.out.println(ele.getKey());
for (Student student : ele.getValue()) {
System.out.println(student.getStuNo() + "\t" + student.getStuName() + "\t" + student.getGender());
}
}
}


/**
* 通过系科对学生分组
*/
public static Map<String, List<Student>> groupStudentsByDept(List<Student> students) {
Map<String, List<Student>> map = new LinkedHashMap<>();
for (Student stu : students) {
String dept = stu.getDeptName();
if (!map.containsKey(dept)) {
List<Student> stus = new ArrayList<>();
stus.add(stu);
map.put(dept, stus);
} else {
map.get(dept).add(stu);
}
}
return map;


}


/**
* 将学生信息对应的字符串转换为学生对象

* @param studentStrs
* @return
*/
public static List<Student> convertStringsToStudents(List<String> studentStrs) {
List<Student> students = new ArrayList<>();
for (String stuStr : studentStrs) {
String[] stuStrs = stuStr.split("\\s+");
Student student = new Student(stuStrs[0], stuStrs[1], stuStrs[2], stuStrs[3]);
students.add(student);
}
return students;
}


/**
* 将文件读取为字符串集合

* @return
*/
public static List<String> readFileToStringList(String filePath) {
List<String> list = new ArrayList<>();
BufferedReader br = null;
InputStreamReader isr = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String line = null;


while ((line = br.readLine()) != null) {
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
return list;
}


}


class Student implements Serializable {


private static final long serialVersionUID = 2756126888689336540L;
private String stuNo;
private String stuName;
private String deptName;
private String gender;


public Student() {


}


public Student(String stuNo, String stuName, String deptName, String gender) {
this.stuNo = stuNo;
this.stuName = stuName;
this.deptName = deptName;
this.gender = gender;
}


public String getStuNo() {
return stuNo;
}


public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}


public String getStuName() {
return stuName;
}


public void setStuName(String stuName) {
this.stuName = stuName;
}


public String getDeptName() {
return deptName;
}


public void setDeptName(String deptName) {
this.deptName = deptName;
}


public String getGender() {
return gender;
}


public void setGender(String gender) {
this.gender = gender;
}


@Override
public String toString() {
return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", deptName=" + deptName + ", gender=" + gender
+ "]";
}


} 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐