BufferedReader read2 = new BufferedReader(new InputStreamReader(new FileInputStream("txt2"))//要读取的文件txt2的路径
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("txt")))//要保持的另外的路径
String line=null
while((line=read2.readLine()!=null){
if(line.equals(read1.readLine())){//判断是否一样
write.writer(line)//写入到第三个文件
write.flush()//刷新
}
}
write.close()
read1.close()
read2.close()
import java.io.BufferedReaderimport java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FilenameFilter
import java.io.IOException
import java.io.InputStreamReader
import java.util.ArrayList
public class Day04_B {
static ArrayList<Integer> list
public static void main(String[] args) {
list = new ArrayList<>()
File dir = new File("K:\\Test")// 文件目录
sum(dir)
}
private static void sum(File dir) {
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".txt")
}
})
for (File file : files) {
read(file)
}
for (int j = list.size() - files.length j < list.size() j++) {
for (int i = 0 i < list.size() - files.length i++) {
System.out.println(list.get(i) + list.get(j))
}
}
}
private static void read(File files) {
BufferedReader br = null
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(files)))
for (String str = br.readLine() str != null str = br.readLine()) {
int tem = 0
String[] strs = str.replaceAll(" ", "").split(",")
for (int i = 0 i < strs.length i++) {
tem += Integer.valueOf(strs[i])// 记录数组,递增和
}
list.add(tem)
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
if (br != null) {
try {
br.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
搂着这个要看情况更具文件名特点来,如果都是AD开头,那么:AD_.*?\.pdf
如果文件名没有特点,但是在路径中:[\\/].*?\.pdf
总之要弄懂原理灵活运用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)