package Self;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileDemo {
public static void main(String[] args) {
BufferedReader br = null;
List
String filepath1 = "E:\\1.txt";
String filepath2 = "E:\\2.txt";
System.out.println(" read file start...");
List
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
filepath1)));
String temp = null;
while ((temp = br.readLine()) != null) {
try {
if (!temp.trim().equals("id"))
list.add(temp);//放第一个文件id对应的值
} catch (Exception e) {
e.printStackTrace();
}
}
br.close();
System.out.println(list.toString());
br = new BufferedReader(new InputStreamReader(new FileInputStream(
filepath2)));
while ((temp = br.readLine()) != null) {//读第二个文件的每一行
try {
if (temp.trim().indexOf("id")==-1) {//判断
String[] strs = temp.split(",");//分割每行的数据 如120401,12669560,02, 2
for (int i = 0; i < list.size(); i++) {
if (strs[1].trim().equals(list.get(i).trim())) {
//遍历list集合,找与分割的数组strs[1]第二个元素12669560有没有在list中
listres.add(strs);
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < listres.size(); i++) {
String[] str = listres.get(i);
for (int j = 0; j < str.length; j++) {
System.out.print(str[j] + " ");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
// System.out.println(listres.size());
System.out.println(" read file end ...");
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception {
File file1 = new File("1.txt");
File file2 = new File("2.txt");
File file3 = new File("3.txt");
List
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
while (br.ready()) {
String line = br.readLine();
list.add(line);
}
br.close();
fr.close();
fr = new FileReader(file2);
br = new BufferedReader(fr);
FileWriter fw = new FileWriter(file3);
while (br.ready()) {
String line = br.readLine();
String[] array = line.split(",");
if (list.contains(array[0])) {
// System.out.println(line);
fw.write(line + "\r\n");
}
}
br.close();
fr.close();
fw.close();
}
}
1先读第二个文件
2 用splite把每行拆成数组。
3 利用数组第二个字段作为key,行的内容或者数组作为value,存入hashmap
读第一个文件,利用map差数据就行了。
public static void main(String[] args) {
BufferedReader br = null;
List
String filepath1 = "F:\\1.txt";
String filepath2 = "F:\\2.txt";
List
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath1)));
String temp = null;
while ((temp = br.readLine()) != null) {
try {
if(!temp.trim().equals("id"))
list.add(temp);
} catch (Exception e) {
e.printStackTrace();
}
}
br.close();
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath2)));
while ((temp = br.readLine()) != null) {
try {
if(!temp.trim().startsWith("id")){
String[] strs = temp.split(",");
for (int i = 0; i < list.size(); i++) {
if(strs[1].trim().equals(list.get(i).trim())){
listres.add(strs);
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < listres.size(); i++) {
String[] str = listres.get(i);
for (int j = 0; j < str.length; j++) {
System.out.print(str[j]+" ");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}