2013-03-22 72 views
1

我有一个问题,一直困扰着我整整一天。我想读取文件的一部分,并将其与同一文件的其他部分进行比较。如果我可以得到某种逻辑关于如何收集第一组线并将其与其他组进行比较,然后移动到下一组并与剩余的组进行比较,直到我将每组相互比较...比较文件的某些部分

我想要完成的是这个阅读前3条线跳过下一条线与下一条3条线比较,跳过下一条线并与下一条3条线比较,然后移动到第2条3条线并与第3条线比较和第四集等等。那么,我试图做的是一样的东西

for{ int i=0; i < file.lenght; i++ 

    for {int j=0; j=i+1; i++{ 

    }} 

但对于一个txt文件

样本文件

this is the way to go 
this is not really it 
what did they say it was 
laughing hahahahahahahaa 
are we on the right path 
this is not really it 
what did they say it was 
smiling hahahahahahahaha 
they are way behind 
tell me how to get therr 
home here we come 
hahahahahahahaha they laught 
are we on the right path 
this is not really it 
what did they say it was 
+0

我明白你用言语说什么,有种的部分 - 你可以说明你正在经历的文件缩减的周期? – Makoto 2013-03-22 03:36:22

+0

该文件是一个日志文件,它有几个部分,表示开始和一些输出,它说结束,它继续以该格式直到文件结束。我想要做的就是检查是否有重复,所以我必须将开始和结束之间的所有输出相互比较,直到文件结束,以确保没有重复。 – user2119847 2013-03-22 04:39:35

回答

1

假设文件装入内存,我会阅读所有线路,用List Files.readAllLines(path,charset)(Java 7),然后将在列表上进行比较

如果文件太大,则创建一个方法

List<String> readLines(Scanner sc, int nLines) { 
    List<String> list = new ArrayList<>(); 
    for (int i = 0; i < nLines; i++) { 
     list.add(sc.nextLine()); 
    } 
    return list; 
} 

,并用它来读/跳过文件

Scanner sc = new Scanner(new File("path")); 
List<String> list1 = readLines(sc, 3); 
readLines(sc, 1); 
List<String> list2 = readLines(sc, 3); 
boolean res = list1.equals(list2) 
+0

该文件将过多,但要全部读取记忆有没有其他办法可以解决它? – user2119847 2013-03-22 04:40:35

+0

好吧,看到更新的答案 – 2013-03-22 04:57:10

+0

,这是非常有用的,但通过文件,我发现他们不是所有3行之前的跳过,有些有4和一些是6行之前,我跳过任何想法 – user2119847 2013-03-22 19:11:55