I’m about to lose my mind on this one right here. I’d like to compare 4 files with each other.
They’re split in 2 folders. So there is folder “A” with file 1-4
and there is folder “B” which is a copy of folder “A”.
package Aufgabe2;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class FTWalker {
FTWalkerTWO two;
public static void main(String[] args) throws IOException {
new FTWalker().walk(args[0]);
}
void walk(String pathname) throws IOException {
File[] files = new File(pathname).listFiles();
List<Integer> list = new ArrayList<Integer>();
List<String> liste2 = new ArrayList<String>();
int i = 0;
if (files != null)
for (File file: files)
if (file.isDirectory())
walk(file.getCanonicalPath());
else{
// process(file.getCanonicalPath(), (int)file.length());
System.out.println(""+file.getName()); //gibt namen aus
list.add( (int) file.length());
liste2.add(file.getName());
i++;
duplikat(file,liste2);
}
// sortiert(list);
}
void process(String name, int length) {
System.out.printf("%-70s%9d%n", name, length);
}
void sortiert(List<Integer> a) {
Collections.sort(a);
for (int k = 0; k < a.size(); k++) {
System.out.print(a.get(k) + " ");
}
System.out.println("");
Comparator<Integer> comparator = Collections.<Integer> reverseOrder();
Collections.sort(a, comparator);
for (int k = 0; k < a.size(); k++) {
System.out.print(a.get(k) + " ");
}
}
public void duplikat(File file,List<String> a ){//stand vorher List<String> liste2 drin
//for(int i.)
if(file.getName().equals(a.get(i)){
System.out.println("Yo\n");
}else{
System.out.println("Sorry seems to be the hardest word\n");
}
}
}
The code above gets a folder at args[0], walks through a directory which contains a couple of files and prints the whole path + the size of the file.
All I want is to see if the files name are equal or not!
I got z.txt in both folders – they are equal (name). Then I changed one of them to z2.txt. Now, I checked both have same name or not. But I don’t get the correct result.
You have several problems in that code.
A naive solution is to replace the List with a Set and then add to it combination of file name and size.
Other solution is to create a object that will contain the file name and the size of it. That will override the implementation of equals() and hashode(), and then use the HashSet to verify that a file of this type had been found.
EDIT:
As you need only check the file name without any other restriction only thing that you could use a Set where you can store the file name. As you are walking through file system you just need to verify that such name is all ready in the set. In addition you need to store the amount of already found file names. To handle this you can used a HashMap where key is file name and Value is the amount.