-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteFileWithCertainExtension.java
113 lines (96 loc) · 3.08 KB
/
DeleteFileWithCertainExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.javamultiplex.filehandling;
import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
public class DeleteFileWithCertainExtension {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter full directory path in this format [drive:\\\\directory] example - c:\\\\abc");
String path = br.readLine();
if (isValidPath(path)) {
File file = new File(path);
if (file.exists()) {
System.out.println("Enter file extension : ");
String extension = br.readLine();
if (isValidExtension(extension)) {
new DeleteFileWithCertainExtension().deleteFiles(path,extension);
} else {
System.out.println("Invalid extension.");
}
} else {
System.out.println("Path doesn't exist.");
}
} else {
System.out.println("Please enter path in format [drive:\\\\directory]");
}
} finally {
if (br != null) {
br.close();
}
}
}
private void deleteFiles(String path, String extension) {
FileExtensionExtractor extractor = new FileExtensionExtractor(extension);
File dir = new File(path);
//list() method internally called accept() method of FilenameFilter interface.
String[] list = dir.list(extractor);
if (list.length == 0) {
return;
}
File fileDelete;
for (String file : list) {
//Creating file path using File.separator
String temp = new StringBuffer(path).append(File.separator).append(file).toString();
fileDelete = new File(temp);
//Deleting file
boolean isdeleted = fileDelete.delete();
System.out.println("file : " + temp + " is deleted : " + isdeleted);
}
}
private static boolean isValidExtension(String extension) {
/*
* Regular expression for validating file extension.
* for example - .txt, .exe, .doc etc
*
*/
String pattern = "^\\.[a-zA-Z]{3}$";
boolean result = false;
if (extension.matches(pattern)) {
result = true;
}
return result;
}
private static boolean isValidPath(String path) {
/*
* Regular expression for validating paths.
* for example - c:\\abc, D:\\mnop etc
*/
String pattern = "^[a-zA-Z]:\\\\\\\\[\\w]*$";
boolean result = false;
if (path.matches(pattern)) {
result = true;
}
return result;
}
/*
* This is an inner class.
* FilenameFilter is an interface, it's implementation is required to fetch files with specific property.
* Here we are fetching files based on extension.
* We have to provide definition of accept() method otherwise it won't work.
*/
public class FileExtensionExtractor implements FilenameFilter {
private String extension;
public FileExtensionExtractor(String extension) {
this.extension = extension;
}
@Override
public boolean accept(File dir, String name) {
//Checking whether file ends with particular extension.
return name.endsWith(extension);
}
}
}