我有一个已经存在的两个文本文件,我想从它们中获取相似的数据并将这些新数据合并到一个文件中。
file.dat
我能够将这两个文本文件的信息复制到一个文本文件中,但是无法通过另一文件中的替代内容来比较每一行中的每个元素!
//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
这就是我要寻找的结果:(注意:“ P_Average”是文件“ info2.txt”中三个Projs的平均值)。
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();
}
}
答案 0 :(得分:0)
通过使用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();
}
}