已知文件 source.txt 中的内容如下
username=root , password= 1234, id=1, level= 10 username= adimin, mima= 1234 , id=2, level= 11 yonghu= xiaoming ,dengji= 12 ,password= 1234,id= 3
-
其中,username、yonghu 都表示用户名,password、mima都表示密码,level、dengji都表示等级
-
在桌面上的这个source.txt文件拷贝到项目 file\data.txt 中(注意:方式不限,不是移动!)
-
读取文本中的数据,将每一行的数据封装成模型,存入 List<User> 中
-
去除集合中id重复的数据,只保留重复id的第一个数据
-
计算这些用户的平均等级、最高等级、最低等级
-
将集合中的数据按照等级降序排序
-
查询集合中等级为[7,15]范围内的第[4,8]个,并将结果按照id升序
-
将这个集合序列化到本地,并反序列化遍历
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
public class homework01 {
public static void main(String[] args) {
//复制文件
// copyToSrc();
List<User> oldList = readFromTxt();
oldList.forEach(System.out::println);
System.out.println("去重id后:");
List<User> newList = removeRepeatId(oldList);
newList.forEach(System.out::println);
IntSummaryStatistics intSummaryStatistics = computeAverOrMaxOrMin(newList);
System.out.println("平均等级="+intSummaryStatistics.getAverage());
System.out.println("最高等级="+intSummaryStatistics.getMax());
System.out.println("最低等级="+intSummaryStatistics.getMin());
newList.sort((u1,u2)->u2.getLevel()-u1.getLevel());
System.out.println("按照等级降序排序后的list:");
newList.forEach(System.out::println);
System.out.println("等级为[7,15]范围内的第[4,8]个,并将结果按照id升序");
newList.stream().sorted(Comparator.comparingInt(User::getId)).filter(ele->ele.getLevel()>=7&&ele.getLevel()<=15).limit(8).skip(3).forEach(System.out::println);
save(newList);
List<User> serializableList = load();
serializableList.sort((n1,n2)->n1.getId()-n2.getId());
System.out.println("反序列化(经过id排序后)");
serializableList.forEach(System.out::println);
}
//反序列化
public static List<User> load(){
List<User> list = new ArrayList<>();
try(ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("src/file/list"))) {
Object obj = inputStream.readObject();
if(obj instanceof List)
list = ((List) obj).subList(0,((List) obj).size());
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return list;
}
//序列化到本地
public static void save(List<User> list){
try(ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("src/file/list"))) {
outputStream.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
}
}
//计算平均等级,最高等级,最低等级
public static IntSummaryStatistics computeAverOrMaxOrMin(List<User> list){
return list.stream().mapToInt(User::getLevel).summaryStatistics();
}
//去除重复
public static List<User> removeRepeatId(List<User> oldList){
Set<User> userSet = new HashSet<>(oldList);
return new ArrayList<>(userSet);
}
/**
* 读取文本中的数据
*/
public static List<User> readFromTxt(){
List<User> list = new ArrayList<>();
try(BufferedReader reader = new BufferedReader(new FileReader("src/file/data.txt"))) {
String line = "";
while((line=reader.readLine())!=null){
//System.out.println(line);
String[] users = line.split(" *, *");
User newUser = new User();
for (String user : users) {
//System.out.println(user);
if(user.startsWith("id")){
newUser.setId(Integer.parseInt(user.split(" *= *")[1]));
}
if(user.startsWith("username")||user.startsWith("yonghu")){
newUser.setUsername(user.split(" *= *")[1]);
}
if(user.startsWith("password")||user.startsWith("mima")){
newUser.setPassword(user.split(" *= *")[1]);
}
if(user.startsWith("level")||user.startsWith("dengji")){
newUser.setLevel(Integer.parseInt(user.split(" *= *")[1]));
}
}
list.add(newUser);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* 拷贝
*/
public static void copyToSrc(){
try(FileChannel srcChannel = FileChannel.open(Paths.get("C:\\Users\\dell\\Desktop\\source.txt"), StandardOpenOption.READ);
FileChannel dstChannel = FileChannel.open(Paths.get("src/file/data.txt"),StandardOpenOption.WRITE,StandardOpenOption.CREATE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(srcChannel.read(buffer)!=-1){
buffer.flip();
dstChannel.write(buffer);
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class User implements Serializable{
private int id;
private String username;
private String password;
private int level;
public User() {
}
public User(int id, String username, String password, int level) {
this.id = id;
this.username = username;
this.password = password;
this.level = level;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", level=" + level +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}