|
| 1 | +package FileCounter; |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.util.*; |
| 6 | +public class duplicate { |
| 7 | +public static int countDirectory=0; |
| 8 | + public static void removeduplicate(File directory) throws Exception{ |
| 9 | + ArrayList<String> fileList = new ArrayList<>(); |
| 10 | + |
| 11 | + for (File file : directory.listFiles()) { |
| 12 | + File file1 = new File(file.getAbsolutePath()); |
| 13 | + FileReader fr = new FileReader(file1); |
| 14 | + BufferedReader reader = new BufferedReader(fr); |
| 15 | + StringBuilder stringBuilder = new StringBuilder(); |
| 16 | + String line = null; |
| 17 | + String ls = System.getProperty("line.separator"); |
| 18 | + while ((line = reader.readLine()) != null) { |
| 19 | + stringBuilder.append(line); |
| 20 | + stringBuilder.append(ls); |
| 21 | + } |
| 22 | + stringBuilder.deleteCharAt(stringBuilder.length() - 1); |
| 23 | + String content = stringBuilder.toString(); |
| 24 | + |
| 25 | + if(fileList.contains(content)) { |
| 26 | + System.out.println("The file "+ file.getName()+" is a duplicate file"); |
| 27 | + System.out.println(file.getName()+ " Removed"); |
| 28 | + file1.delete(); |
| 29 | + System.out.println(""); |
| 30 | + } |
| 31 | + else { |
| 32 | + fileList.add(content); |
| 33 | + } |
| 34 | + reader.close(); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + public static void main(String args[]) throws Exception { |
| 40 | + Scanner s = new Scanner(System.in); |
| 41 | + System.out.println("Enter the Path of your directory : "); |
| 42 | + String path = s.next(); |
| 43 | + File file = new File(path); |
| 44 | + duplicate.removeduplicate(file); |
| 45 | + System.out.println("All Duplicates File Removed"); |
| 46 | + s.close(); |
| 47 | + } |
| 48 | +} |
0 commit comments