java 比较两个文件并确定字段编号是否有任何差异
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 比较两个文件并确定字段编号是否有任何差异相关的知识,希望对你有一定的参考价值。
package com.ck.run;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Created by s4553711 on 2017/7/12.
*/
public class VCFReader {
public static void main(String[] args) {
if (args.length == 0) return;
//System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "4");
System.out.println("Available threads: "+Runtime.getRuntime().availableProcessors());
callzip(args[0], args[1], Integer.valueOf(args[2]), Integer.valueOf(args[3]));
}
private static void callzip(String fileA, String fileB, Integer st, Integer end) {
Stream<String> streamA;
Stream<String> streamB;
try {
streamA = Files.lines(Paths.get(fileA));
streamB = Files.lines(Paths.get(fileB));
zip(streamA, streamB, (a, b) -> a + ":::" + b).parallel().forEach((String s) -> {
String[] source = s.split(":::");
String[] colA = source[0].split("\",\"");
String[] colB = source[1].split("\",\"");
for(int i = st; i <= end; i++) {
if (!colA[i].equals(colB[i])) {
System.out.println("DIFF > col: "+i+", A: "+colA[i]+", B: "+colB[i]);
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
// copy from the discussion: https://stackoverflow.com/questions/17640754/zipping-streams-using-jdk8-with-lambda-java-util-stream-streams-zip
public static<A, B, C> Stream<C> zip(Stream<? extends A> a,
Stream<? extends B> b,
BiFunction<? super A, ? super B, ? extends C> zipper) {
Objects.requireNonNull(zipper);
Spliterator<? extends A> aSpliterator = Objects.requireNonNull(a).spliterator();
Spliterator<? extends B> bSpliterator = Objects.requireNonNull(b).spliterator();
// Zipping looses DISTINCT and SORTED characteristics
int characteristics = aSpliterator.characteristics() & bSpliterator.characteristics() &
~(Spliterator.DISTINCT | Spliterator.SORTED);
long zipSize = ((characteristics & Spliterator.SIZED) != 0)
? Math.min(aSpliterator.getExactSizeIfKnown(), bSpliterator.getExactSizeIfKnown())
: -1;
Iterator<A> aIterator = Spliterators.iterator(aSpliterator);
Iterator<B> bIterator = Spliterators.iterator(bSpliterator);
Iterator<C> cIterator = new Iterator<C>() {
@Override
public boolean hasNext() {
return aIterator.hasNext() && bIterator.hasNext();
}
@Override
public C next() {
return zipper.apply(aIterator.next(), bIterator.next());
}
};
Spliterator<C> split = Spliterators.spliterator(cIterator, zipSize, characteristics);
return (a.isParallel() || b.isParallel())
? StreamSupport.stream(split, true)
: StreamSupport.stream(split, false);
}
}
以上是关于java 比较两个文件并确定字段编号是否有任何差异的主要内容,如果未能解决你的问题,请参考以下文章