Optimize the file content comparison

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












Program to compare if files within two directories are having same content
I followed the approach of getting the list of files under two directories recursively and store it in different list with it is canonical path.



Then using two loops, compared the each file line by line. Below is my code,



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileCompare

static List<String> dir1_filePath = new ArrayList<String>();
static List<String> dir2_filePath = new ArrayList<String>();
static void fetchFiles(File arr,int start,int level, List<String> filePath) throws IOException

if(start == arr.length)
return;
if(arr[start].isFile())
filePath.add(arr[start].getCanonicalPath());

else if(arr[start].isDirectory())

fetchFiles(arr[start].listFiles(), 0, level + 1,filePath);

fetchFiles(arr,++start, level,filePath);


public static void main(String args) throws IOException

Scanner s = new Scanner(System.in);
String directory_one = s.next();
String directory_two = s.next();
File dir_one = new File(directory_one);
File dir_two = new File(directory_two);

if(dir_one.exists() && dir_one.isDirectory())

File dir1_files = dir_one.listFiles();
fetchFiles(dir1_files,0,0, dir1_filePath);

if(dir_two.exists() && dir_two.isDirectory())

File dir2_files = dir_two.listFiles();
fetchFiles(dir2_files,0,0, dir2_filePath);

boolean fileEqual;
for(int i=0;i<dir1_filePath.size();i++)
for(int j=1; j<dir2_filePath.size();j++)
fileEqual = compareTwoFiles(dir1_filePath.get(i), dir2_filePath.get(j));
if(fileEqual)
System.out.println(dir1_filePath.get(i) + " and " + dir2_filePath.get(i) + " are Equal");





private static boolean compareTwoFiles(String file1, String file2) throws IOException



Is there any other approach to compare the files effectively, instead of using two loops?







share|improve this question



















  • Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory?
    – DodgyCodeException
    Mar 19 at 18:04










  • How equal? if absolutely equal, then the first check would be file size.
    – AJD
    Mar 19 at 18:54






  • 1




    You could also create a hash of the case insensitive contents and compare the hashes.
    – AJD
    Mar 19 at 18:56










  • @DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory)
    – irs102info
    Mar 20 at 3:25
















up vote
0
down vote

favorite












Program to compare if files within two directories are having same content
I followed the approach of getting the list of files under two directories recursively and store it in different list with it is canonical path.



Then using two loops, compared the each file line by line. Below is my code,



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileCompare

static List<String> dir1_filePath = new ArrayList<String>();
static List<String> dir2_filePath = new ArrayList<String>();
static void fetchFiles(File arr,int start,int level, List<String> filePath) throws IOException

if(start == arr.length)
return;
if(arr[start].isFile())
filePath.add(arr[start].getCanonicalPath());

else if(arr[start].isDirectory())

fetchFiles(arr[start].listFiles(), 0, level + 1,filePath);

fetchFiles(arr,++start, level,filePath);


public static void main(String args) throws IOException

Scanner s = new Scanner(System.in);
String directory_one = s.next();
String directory_two = s.next();
File dir_one = new File(directory_one);
File dir_two = new File(directory_two);

if(dir_one.exists() && dir_one.isDirectory())

File dir1_files = dir_one.listFiles();
fetchFiles(dir1_files,0,0, dir1_filePath);

if(dir_two.exists() && dir_two.isDirectory())

File dir2_files = dir_two.listFiles();
fetchFiles(dir2_files,0,0, dir2_filePath);

boolean fileEqual;
for(int i=0;i<dir1_filePath.size();i++)
for(int j=1; j<dir2_filePath.size();j++)
fileEqual = compareTwoFiles(dir1_filePath.get(i), dir2_filePath.get(j));
if(fileEqual)
System.out.println(dir1_filePath.get(i) + " and " + dir2_filePath.get(i) + " are Equal");





private static boolean compareTwoFiles(String file1, String file2) throws IOException



Is there any other approach to compare the files effectively, instead of using two loops?







share|improve this question



















  • Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory?
    – DodgyCodeException
    Mar 19 at 18:04










  • How equal? if absolutely equal, then the first check would be file size.
    – AJD
    Mar 19 at 18:54






  • 1




    You could also create a hash of the case insensitive contents and compare the hashes.
    – AJD
    Mar 19 at 18:56










  • @DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory)
    – irs102info
    Mar 20 at 3:25












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Program to compare if files within two directories are having same content
I followed the approach of getting the list of files under two directories recursively and store it in different list with it is canonical path.



Then using two loops, compared the each file line by line. Below is my code,



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileCompare

static List<String> dir1_filePath = new ArrayList<String>();
static List<String> dir2_filePath = new ArrayList<String>();
static void fetchFiles(File arr,int start,int level, List<String> filePath) throws IOException

if(start == arr.length)
return;
if(arr[start].isFile())
filePath.add(arr[start].getCanonicalPath());

else if(arr[start].isDirectory())

fetchFiles(arr[start].listFiles(), 0, level + 1,filePath);

fetchFiles(arr,++start, level,filePath);


public static void main(String args) throws IOException

Scanner s = new Scanner(System.in);
String directory_one = s.next();
String directory_two = s.next();
File dir_one = new File(directory_one);
File dir_two = new File(directory_two);

if(dir_one.exists() && dir_one.isDirectory())

File dir1_files = dir_one.listFiles();
fetchFiles(dir1_files,0,0, dir1_filePath);

if(dir_two.exists() && dir_two.isDirectory())

File dir2_files = dir_two.listFiles();
fetchFiles(dir2_files,0,0, dir2_filePath);

boolean fileEqual;
for(int i=0;i<dir1_filePath.size();i++)
for(int j=1; j<dir2_filePath.size();j++)
fileEqual = compareTwoFiles(dir1_filePath.get(i), dir2_filePath.get(j));
if(fileEqual)
System.out.println(dir1_filePath.get(i) + " and " + dir2_filePath.get(i) + " are Equal");





private static boolean compareTwoFiles(String file1, String file2) throws IOException



Is there any other approach to compare the files effectively, instead of using two loops?







share|improve this question











Program to compare if files within two directories are having same content
I followed the approach of getting the list of files under two directories recursively and store it in different list with it is canonical path.



Then using two loops, compared the each file line by line. Below is my code,



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileCompare

static List<String> dir1_filePath = new ArrayList<String>();
static List<String> dir2_filePath = new ArrayList<String>();
static void fetchFiles(File arr,int start,int level, List<String> filePath) throws IOException

if(start == arr.length)
return;
if(arr[start].isFile())
filePath.add(arr[start].getCanonicalPath());

else if(arr[start].isDirectory())

fetchFiles(arr[start].listFiles(), 0, level + 1,filePath);

fetchFiles(arr,++start, level,filePath);


public static void main(String args) throws IOException

Scanner s = new Scanner(System.in);
String directory_one = s.next();
String directory_two = s.next();
File dir_one = new File(directory_one);
File dir_two = new File(directory_two);

if(dir_one.exists() && dir_one.isDirectory())

File dir1_files = dir_one.listFiles();
fetchFiles(dir1_files,0,0, dir1_filePath);

if(dir_two.exists() && dir_two.isDirectory())

File dir2_files = dir_two.listFiles();
fetchFiles(dir2_files,0,0, dir2_filePath);

boolean fileEqual;
for(int i=0;i<dir1_filePath.size();i++)
for(int j=1; j<dir2_filePath.size();j++)
fileEqual = compareTwoFiles(dir1_filePath.get(i), dir2_filePath.get(j));
if(fileEqual)
System.out.println(dir1_filePath.get(i) + " and " + dir2_filePath.get(i) + " are Equal");





private static boolean compareTwoFiles(String file1, String file2) throws IOException



Is there any other approach to compare the files effectively, instead of using two loops?









share|improve this question










share|improve this question




share|improve this question









asked Mar 19 at 17:05









irs102info

132




132











  • Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory?
    – DodgyCodeException
    Mar 19 at 18:04










  • How equal? if absolutely equal, then the first check would be file size.
    – AJD
    Mar 19 at 18:54






  • 1




    You could also create a hash of the case insensitive contents and compare the hashes.
    – AJD
    Mar 19 at 18:56










  • @DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory)
    – irs102info
    Mar 20 at 3:25
















  • Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory?
    – DodgyCodeException
    Mar 19 at 18:04










  • How equal? if absolutely equal, then the first check would be file size.
    – AJD
    Mar 19 at 18:54






  • 1




    You could also create a hash of the case insensitive contents and compare the hashes.
    – AJD
    Mar 19 at 18:56










  • @DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory)
    – irs102info
    Mar 20 at 3:25















Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory?
– DodgyCodeException
Mar 19 at 18:04




Are you trying to compare every file in directory 1 with every single file in directory 2, thus making N x M comparisons? Or are you meant to only compare files with the same name and in the same subdirectory?
– DodgyCodeException
Mar 19 at 18:04












How equal? if absolutely equal, then the first check would be file size.
– AJD
Mar 19 at 18:54




How equal? if absolutely equal, then the first check would be file size.
– AJD
Mar 19 at 18:54




1




1




You could also create a hash of the case insensitive contents and compare the hashes.
– AJD
Mar 19 at 18:56




You could also create a hash of the case insensitive contents and compare the hashes.
– AJD
Mar 19 at 18:56












@DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory)
– irs102info
Mar 20 at 3:25




@DodgyCodeException, Yes I want to compare the same content in files from two different directories. Not comparing with the name, with the content of the files from different directories.(Even in sub directory)
– irs102info
Mar 20 at 3:25















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189951%2foptimize-the-file-content-comparison%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189951%2foptimize-the-file-content-comparison%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation