package com.ncs.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
public class GetDomainName {
/**
*
* @param folderPath IP路径
* @param checkPath 检索路径
* @param outPath 输出路径(需创建一个空文件)
*/
public static void getDnsIp(String folderPath,String checkPath,String outPath) {
int line=0;
int line1=0;
ArrayList<String> listIp = new ArrayList<>();
try {
//System.out.println("读取文件");
// FileWriter fw = new FileWriter(outPath);
File file = new File(folderPath);
if(file.isFile()) {
// System.out.println("获取文件"+file.getPath());
BufferedReader reader = new BufferedReader(new FileReader(file));
String tempString=null;
while ((tempString = reader.readLine()) != null) {
line++;
System.out.println("第"+line+"个数据");
/*检索域名*/
listIp.add(tempString);
}
File file2 = new File(checkPath);
if(file2.isDirectory()) {
// System.out.println("取得检索文件夹:"+file2.getPath());
File[] tempList = file2.listFiles();
// System.out.println("文件总数:"+tempList.length);
for (File file3:tempList) {
if(file3.isFile()) {
line1++;
System.out.println("第"+line1+"个文件");
BufferedReader reader1 = new BufferedReader(new FileReader(file3));
String tempString1=null;
// System.out.println("检索IP结果:");
while((tempString1 = reader1.readLine()) != null) {
String[] split = tempString1.split("\\|");
// System.out.println("域名:"+split[0]);
String str=null;
for (int i = 0; i < listIp.size(); i++) {
if(split[2].equals(listIp.get(i)) ) {
//System.out.println("第"+line1+"行"+tempString1);
// System.out.println("域名:"+split[0]+"IP:"+tempString);
str=split[0]+"|"+listIp.get(i)+"\r\n";
FileWriter fw = new FileWriter(outPath, true);
fw.write(str+"\r\n");
fw.close();
}
}
}
reader1.close();
}else {
System.out.println("出错");
return;
}
}
}
reader.close();
}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("结束"+line1);
}
public static void main(String[] args) {
getDnsIp("C:\\Users\\zmz\\Desktop\\a.txt","C:\\Users\\zmz\\Desktop\\cqdx","C:\\Users\\zmz\\Desktop\\s.txt");
}
}
看你的代码,你是要实现从第一个文件中进行读取行,然后从第二个文件夹中进行读取文件个数,可能是你的文件夹中存在文件夹中还有文件,需要进行递归。不过这个代码确实写得太糟糕了
希望一楼指点一二,我新手,谢谢您