Optimize the file content comparison
Clash 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?
java algorithm programming-challenge file
add a comment |Â
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?
java algorithm programming-challenge file
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
add a comment |Â
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?
java algorithm programming-challenge file
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?
java algorithm programming-challenge file
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
add a comment |Â
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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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