gpt4 book ai didi

java - java中如何比较两个txt文件中的元素并将它们合并到一个文件中?

转载 作者:行者123 更新时间:2023-12-02 05:45:31 24 4
gpt4 key购买 nike

我已经存在两个文本文件,我想从中获取相似的数据并将这些新数据合并到一个文件中。

例如:

//info1.txt
ID,Name,LastName,WorkPlace
12345,James,Jone,Us
34563,Mike,Smith,Canada
34523,Matt,jk,Uk

//info2.txt
ID,Proj1,Proj2,Proj3,Assignment,Final
34563,60,100,75,89,50
34523,70,56,75,100,70
12345,100,90,75,89,100

我能够将这两个文本文件的信息复制到一个文本文件中,但无法将每一行中的每个元素与另一个文件中的替代元素进行比较!

我的代码:

    import java.io.*; 

public class Merge {
public static void main(String[] args) throws IOException {

//PrintWriter object for info3.txt
PrintWriter pw = new PrintWriter("info3.txt");

//BufferedReader object for info1.txt & info2.txt
BufferedReader br1 = new BufferedReader(new FileReader("info1.txt"));
BufferedReader br2 = new BufferedReader(new FileReader("info2.txt"));

String line1 = br1.readLine();
String line2 = br2.readLine();

//loop to copy each lines of the two files to info3.txt
while (line1 != null || line2 !=null) {
if(line1 != null) {
pw.println(line1);
line1 = br1.readLine();
}
if(line2 != null) {
pw.println(line2);
line2 = br2.readLine();
}
}
pw.flush();
br1.close();
br2.close();
pw.close();
}
}

这就是我正在寻找的结果:(注意:“P_Average”是文件“info2.txt”中三个 Projs 的平均值)。

//merged_file.txt
ID,Name,P_Average,Assignment,Final,WorkPlace
12345,James,88.3,89,100,Us
34523,Matt,67.0,100,70,Uk
34563,Mike,78.3,89,50,Canada

最佳答案

通过使用 java 8 的 Files API,

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Merge {

public static void main(String[] args) throws IOException {

List<String> students = Files.readAllLines(Paths.get("info1.txt"));
List<String> grades = Files.readAllLines(Paths.get("info2.txt"));

List<String> results = new ArrayList<>();

results.add("ID,Name,P_Average,Assignment,Final,WorkPlace");

// remove header lines
students.remove(0);
grades.remove(0);

for(String student : students) {
String[] s = student.split(",");
for(String grade : grades) {
String[] g = grade.split(",");
if(Objects.equals(s[0], g[0])) {
results.add(s[0] + "," + s[1] + "," + ((Integer.parseInt(g[1]) + Integer.parseInt(g[2]) + Integer.parseInt(g[3]))/3) + "," + g[4] + "," + g[5] + "," + s[3]);
}
}
}

Files.write(Paths.get("info3.txt"), results);

}
}

另一种简单的方法是使用数据类(Student)和数据结构(Map)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class Merge {

static class Student {
String ID,Name,LastName,WorkPlace,Proj1,Proj2,Proj3,Assignment,Final;

@Override
public String toString() {
return ID + "," + Name + "," + ((Integer.parseInt(Proj1) + Integer.parseInt(Proj2) + Integer.parseInt(Proj3))/3) + "," + Assignment + "," + Final + "," + WorkPlace;
}
}

public static void main(String[] args) throws IOException {

// PrintWriter object for info3.txt
PrintWriter pw = new PrintWriter("info3.txt");

// BufferedReader object for info1.txt & info2.txt
BufferedReader br1 = new BufferedReader(new FileReader("info1.txt"));
BufferedReader br2 = new BufferedReader(new FileReader("info2.txt"));

// skip header lines
br1.readLine();
br2.readLine();

Map<String, Student> map = new HashMap<>();

String line;
while((line = br1.readLine()) != null) {
String[] data = line.split(",");
Student s = new Student();
s.ID = data[0];
s.Name = data[1];
s.LastName = data[2];
s.WorkPlace = data[3];
map.put(s.ID, s);
}


while((line = br2.readLine()) != null) {
String[] data = line.split(",");
Student s = map.get(data[0]);
s.Proj1 = data[1];
s.Proj2 = data[2];
s.Proj3 = data[3];
s.Assignment = data[4];
s.Final = data[5];
}

pw.println("ID,Name,P_Average,Assignment,Final,WorkPlace");

map.values().forEach( pw::println );

pw.flush();
br1.close();
br2.close();
pw.close();
}
}

关于java - java中如何比较两个txt文件中的元素并将它们合并到一个文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com