【发布时间】:2017-01-25 09:41:04
【问题描述】:
我有两个.dimacs 格式的文件,例如:
c example_01.cnf
p cnf 6 9
1 0
-2 1 0
-1 2 0
-5 1 0
-6 1 0
-3 2 0
-4 2 0
-3 -4 0
3 4 -2 0
和,
c example_02.cnf
p cnf 9 6
-7 2 0
7 -2 0
-8 3 0
8 -3 0
-9 4 0
9 -4 0
我想将文件 example_01.cnf 与 example_02.cnf 进行比较,以便仅从文件 example_01.cnf 中提取具有相似值的那些行(在任何行)从文件example_02.cnf,并将结果保存在新文件中,例如example_result.cnf。
在这种情况下,example_result.cnf 将如下所示:
c example_result.cnf
p cnf 4 6
-2 1 0
-1 2 0
-3 2 0
-4 2 0
-3 -4 0
3 4 -2 0
例如,1 0、-5 1 0 和-6 1 0 行不在结果文件中,因为1、5 和6 中没有一个数字在example_02.cnf 中。
我当前的代码是:
import scala.io.Source
object Example_01 {
val source = Source.fromFile("example_01.cnf")
val source2 = Source.fromFile("example_02.cnf")
val destination = new PrintWriter(new File("example_result.cnf"))
def main(args: Array[String]): Unit = {
var nrVariables: Int = 0
var nrLines: Int = 0
destination.write("c example_result.cnf \n")
destination.write("p cnf " + nrVariables + " " + nrLines + "\n") //not finished!
/* How I can compare the all the numbers from the second file 'source2' like in the 'if' statement below? */
for(line <- source.getLines()) ; if line.contains("2") & line.contains("0") ) {
destination.write(line)
destination.write("\n")
nrLines += 1
}
source.close()
destination.close()
}
在这段代码中,我还没有使用第二个文件example_02.cnf。如何比较这两个文件?
【问题讨论】:
标签: scala file comparison file-comparison filecompare