gpt4 book ai didi

java - 比较两个不同文本文件中的行并修改 d 目标行的最后一列

转载 作者:行者123 更新时间:2023-12-01 10:57:30 26 4
gpt4 key购买 nike

我是一个java初学者......我想使用文件2中每一行的第一列作为标准来检查文件1中是否存在这样的行..如果它确实改变了最后一列( yes, no) 将文件 1 中的这一行改为“也许”。

File 1 
6130124,860847,9,9,4,9,e,6,2,S, yes
6150744,194559,7,7,4,9,e,6,2,S,no
6065543,511353,5,4,4,9,e,6,2,S,no
8419554,261221,6,6,2,18,a,7,1,S,yes
8190063,144544,5,5,2,18,a,7,1,S,no
8276868,285387,4,4,2,18,a,7,1,S,no
625541,233528,2,2,4,9,a,3,1,N,yes
676477,138280,2,2,4,9,a,3,1,N,no
628496,59404,2,2,4,9,a,3,1,N,no

File 2 ( a subset of File 1)
6130124,860847,9,9,4,9,e,6,2,S, yes
6150744,194559,7,7,4,9,e,6,2,S,no
8276868,285387,4,4,2,18,a,7,1,S,no
625541,233528,2,2,4,9,a,3,1,N,yes

这意味着...文件 1 将打印为:

6130124,860847,9,9,4,9,e,6,2,S, maybe
6150744,194559,7,7,4,9,e,6,2,S, maybe
6065543,511353,5,4,4,9,e,6,2,S,no
8419554,261221,6,6,2,18,a,7,1,S,yes
8190063,144544,5,5,2,18,a,7,1,S,no
8276868,285387,4,4,2,18,a,7,1,S,maybe
625541,233528,2,2,4,9,a,3,1,N,mabe
676477,138280,2,2,4,9,a,3,1,N,no
628496,59404,2,2,4,9,a,3,1,N,no

这就是我到目前为止所做的

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;



public class ComparingBothFiles {


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

PrintWriter out = null;
BufferedReader br = null;
BufferedReader br2 = null;
String temp = null;
try {
br = new BufferedReader(new FileReader("File2.txt"));

out = new PrintWriter(new BufferedWriter(new FileWriter(
"Me.txt", true))); //the file you write in

String line = br.readLine();

while (!line.isEmpty()) {
String ID = line.split(",")[0];
if (!ID.equals(temp)) {
try {
br2 = new BufferedReader(
new FileReader("File1.txt")); //the proper database
String line2 = br2.readLine();
while (!line2.isEmpty()) {
String ID2 = line2.split(",")[0];
if (ID2.equals(ID)) {
out.println(line2);
//erase the line you've just written if you want
}
line2 = br2.readLine();
}
br2.close();
} catch (Exception e) {
System.out.println("error");
} finally {
if (br2 != null) {
br2.close();
}
}
}
line = br.readLine();
temp = ID;
}


out.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (NullPointerException e) {
System.out.println("null");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("error");

} finally {
if (out != null) {
out.close();
}
if (br != null) {
br.close();
}
}

}

最佳答案

首先推荐 br.readLine() 在到达文件末尾时返回 null,所以

String line = br.readLine();
while(line != null){
line = br.breadLine();
}

将您读到的行存储在 List<List<String>>;

将您的值存储在List<String>;中的一行中

这是一个工作代码

public class Main {

public static void main(String[] args) throws
Exception {
List<List<String>> firstFileLines = readFileLines("file1");
List<List<String>> secondFileLines = readFileLines("file2");

int column = 0;
for (List<String> line : firstFileLines) {
String columnValue = line.get(column);
boolean valueMatch = false;
for (List<String> secondFileLine : secondFileLines) {
if (columnValue.equals(secondFileLine.get(column))) {
valueMatch = true;
break;
}
}
if (valueMatch) {
int lastIndex = line.size() - 1;
line.set(lastIndex, "REPLACE_ME");
}
System.out.println(String.join(",", line));
}
}

private static List<List<String>> readFileLines(String file)
throws Exception {
BufferedReader br1 = new BufferedReader(new FileReader(file));
List<List<String>> fileLines = new ArrayList<>();

String line = br1.readLine();
while (line != null) {
List<String> valuesFromLine = new ArrayList<>();
String[] values = line.split(",");
for (String val : values) {
valuesFromLine.add(val.trim());
}
fileLines.add(valuesFromLine);

line = br1.readLine();
}

br1.close();
return fileLines;
}
}

这是我复制的文件的输出

6130124,860847,9,9,4,9,e,6,2,S,REPLACE_ME
6150744,194559,7,7,4,9,e,6,2,S,REPLACE_ME
6065543,511353,5,4,4,9,e,6,2,S,no
8419554,261221,6,6,2,18,a,7,1,S,yes
8190063,144544,5,5,2,18,a,7,1,S,no
8276868,285387,4,4,2,18,a,7,1,S,REPLACE_ME
625541,233528,2,2,4,9,a,3,1,N,REPLACE_ME
676477,138280,2,2,4,9,a,3,1,N,no
628496,59404,2,2,4,9,a,3,1,N,no

关于java - 比较两个不同文本文件中的行并修改 d 目标行的最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33546139/

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