简体   繁体   English

在行数不相等的情况下读取多个文件

[英]Multiple File reading where number of rows are not equal

I have two data files. 我有两个数据文件。 Data are double type (eg, 90.0, 25.63). 数据是双精度类型(例如90.0、25.63)。 File1 consists of 3 columns and several rows and File2 consists of 4 columns and several rows. File1由3列和几行组成,而File2由4列和几行组成。 I read data from both files separately in one Java program and the Column1 data of File1 is matched with the Column1 data of File2, then a message will display that data is matched else data is not matched. 我在一个Java程序中分别从两个文件中读取数据,并且File1的Column1数据与File2的Column1数据匹配,然后一条消息将显示数据已匹配,否则数据不匹配。 The number of rows in the first data file is not same of the number of rows of the second data file. 第一数据文件中的行数与第二数据文件中的行数不同。 If there is a match found in row number 4 then the data before 4th row will write as it is and in fourth row it will write down There is match . 如果在行号4找到了一个匹配,那么第4行之前的数据会写,因为它是和第四行中它会记下There is match

Example: 例:

File1: 文件1:

 2.0   0.6258  0.239  1.852
 3.0   0.5289  0.782  2.358
 5.0   1.2586  2.3658 0.1258
 6.0   0.235   0.8547 3.5870

File2: 文件2:

5.0  0.8974  1.2358  0.2581  
7.0  0.3258  0.6528  0.6987

Number of rows in File2 is lower than in File1. File2中的行数低于File1中的行数。 All the data of 1st column for both File1 and File2 is in ascending order. File1和File2的第一列的所有数据都按升序排列。

I want to write 2.0 and 3.0 as it is. 我想按原样编写2.03.0 Then there is a match found from File2 so it will write "Match found", then 6.0 from File1 is written as it is. 然后从File2找到一个匹配项,因此它将写入“ Match found”,然后按原样写入File1中的6.0 Then again searching if a match is found, then again write down "Match found". 然后再次搜索是否找到匹配项,然后再次写下“找到匹配项”。

Code: 码:

import java.io.File;
import java.util.Scanner;

public class F1 {
    public static void main(String args[])throws Exception{
        Scanner Y =new Scanner(new File("C:\\File1.txt"));
        Scanner X =new Scanner(new File("C:\\File.txt"));
        double a=0.0,b=0.0,c,d=0.0,e=0.0,f,g,h;
        while (X.hasNext() && Y.hasNext()) {
            a = X.nextDouble();
            System.out.println(a);//1st row of file1,2nd row of file1.... So lastly some rows at the end of the file will be discard
            b = X.nextDouble();

            c = X.nextDouble();

            d = Y.nextDouble();
            System.out.println(e);// 1st row of file2. Every row print as the number of row is less than File1.
            e = Y.nextDouble();

            f = Y.nextDouble();
            g = Y.nextDouble();

            if(a==d) {
                System.out.println("They are matched");
            }
            else{
                System.out.println("Not Matched");
            }
        }
    }
}

Do I need to implement any search procedure? 我需要执行任何搜索程序吗? Like Binarsearch? 喜欢Binarsearch? In Java there is a procedure Arrays.Binarysearch(array,key) . 在Java中,有一个过程Arrays.Binarysearch(array,key) So to implement that I need to store the a variable in an array. 因此,要实现我需要存储a变量数组。 Then each cell of this array will be compared with d . 然后将该数组的每个单元与d进行比较。 Is that correct procedure? 那是正确的程序吗?

The approach you have chosen will work if the files are either sorted by the first column or are otherwise guaranteed to have the same order of elements. 如果文件按第一列排序或保证具有相同顺序的元素,则选择的方法将起作用。

If that is not the case you must first read the files into memory (eg a List<Double> per file) and compare the contents of the list, for example by sorting the lists and then comparing element for element (while remembering that floating point comparisons might be tricky). 如果不是这种情况,则必须首先将文件读入内存(例如,每个文件为List<Double> )并比较列表的内容,例如,通过对列表进行排序,然后将元素与元素进行比较(同时记住该浮点数)比较可能比较棘手)。 If the values are guaranteed to be unique per file you could use a Set per file and directly compare the sets. 如果保证每个文件的值都是唯一的,则可以使用“每个文件Set并直接比较设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM