-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSearchApplication.java
268 lines (216 loc) · 7.85 KB
/
FileSearchApplication.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* file > new > java project > add a new class > run as > run configurations
* Esegui app da console con 2 param in input:
* run a java application passing two or more arguments to the command line:
* project > build project : disable
* tx dx sulla classe / progetto : run as > run configuration > arguments: nella box, inserire i param separati da spazi.
* tali param verranno passati al main() nell'array args[]. es args[0], args[1]
*
* corso by Julian Robichaux - LinkedIn
*
*
*
* + java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
* + java.nio.charset.MalformedInputException: Input length = 1 : binary file
*
* Pattern class precompile the regularexpression to optimize memory & use over and over againg without compile it every time that you use it.
*
* */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileSearchApplication {
String path;
String regex;
String zipFileName;
List <File> zipFiles = new ArrayList <File>() ;
Pattern pattern ;
public static void main(String[] args) {
FileSearchApplication app = new FileSearchApplication();
System.out.println("args - lenght : " + args.length);
int valutazione = Math.min(args.length, 3);
System.out.println("valutazione : " + valutazione);
/**
* l�esecuzione letteralmente salta alla riga di codice etichettata con il valore corrispondente al valore dell�argomento di switch e da quella posizione continua eseguendo tutte le istruzioni senza tener conto di eventuali case.
* Ad esempio:
int c = ...;
switch (c) {
case 1:
System.out.print("1 ");
case 2:
System.out.print("2 ");
break;
case 3:
System.out.println("3 ");
case 4:
System.out.println("4 ");
default:
System.out.println("4+");
}
Copy
Facciamo una tabella in cui mettiamo i diversi risultati stampati sulla console al cambiare del valore di c:
Valore di �c� Stampa sulla console
3 : 3 4 4+
1 : 1 2
return or break ?
salta a quello specifico case, eseguendo tutto quello che c'e' dopo e
FREGANDOSENE di tutti i CASE, quindi a cascata, a meno di non incontrare BREAK / RETURN
* */
switch (valutazione) { // only 3 command arguments
case 0: // no arguments at all
System.out.println("USAGE: FileSearchApplication path [regex]");
return ;
case 3: // the third argument
app.setZipFileName(args[2]);
System.out.println("zipfilename : [" + app.getZipFileName() + "]");
case 2: // the second argument
app.setRegex(args[1]);
System.out.println("regex : [" + app.getRegex() + "]");
case 1: // the first argument
app.setPath(args[0]);
System.out.println("path : [" + app.getPath()+ "]");
default:
System.out.println("searching...");
}
try {
app.walkDirectory(app.getPath()) ;
} catch (Exception e) {
e.printStackTrace();
}
}
public void walkDirectory (String path) throws IOException {
this.walkDirectoryJava8(path);
this.zipFilesJava7();
}
public void addFileToZip(File file) {
if (getZipFileName() != null)
zipFiles.add(file);
}
public String getRelativeFileName(File file, File baseDir) {
String fileName = file.getAbsolutePath().substring(baseDir.getAbsolutePath().length());
fileName = fileName.replace('\\', '/'); // zipentryfile use "/"
while (fileName.startsWith("/")) {
fileName = fileName.substring(1);
}
return fileName;
}
// using FileScanner
// public void walkDirectoryJava6 (String path) throws IOException {
// System.out.println("sono in walkDirectoryJava6() : " + path);
// File directory = new File (path);
// File [] filesInThisDirectory = directory.listFiles() ; // list both files & subdirectories that exists in this path. NOT: all files in the founded subdirectory.
//
// for (File eachFile : filesInThisDirectory) {
// if (eachFile.isDirectory())
// walkDirectoryJava6(eachFile.getAbsolutePath());
// else
// processFile(eachFile); // todo with the file
// }
// }
// public void walkDirectoryJava7 (String path) throws IOException {
// Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
// @Override
// public FileVisitResult visitFile (Path file, BasicFileAttributes a)
// throws IOException {
// processFile(file.toFile());
// return FileVisitResult.CONTINUE;
// }
// });
// }
// to process a file, use Streams + Lambda functions
public void walkDirectoryJava8 (String path) throws IOException {
System.out.println("walkDirectoryJava8() : " + path);
Files.walk(Paths.get(path)) // return a Stream
.forEach( f -> processFile (f.toFile())); // LAMBDA EXPRESSION : operate on each item on the Stream
}
/**
* this method remain the same for all versions of java.
*
* */
public void processFile(File file) {
System.out.println("processFile() - file : " + file);
try {
if (searchFile(file)) {
System.out.println(" ---> file match: " + file);
addFileToZip(file);
}
} catch (IOException | UncheckedIOException e) {
System.out.println("processFile () - " + file + " - " + e.getMessage());
}
}
public boolean searchFile(File file) throws IOException {
return searchFileJava8(file) ;
}
// open & read each file
/**
* anyMatch: case insensitive, recursive
* */
public boolean searchFileJava8(File file) throws IOException {
return Files.lines(file.toPath(), StandardCharsets.UTF_8)
.anyMatch(t -> searchText(t)); // anyMatch: short circuiting method: return true as soon as find the searched one
}
public boolean searchText(String text) {
//return (this.getRegex() == null) ? true :
// text.toLowerCase().contains(this.getRegex().toLowerCase()); // case insensitive search
return searchText2(text);
}
/**find if the string contains this term:
* here find if the fileName contains searched input regex*/
public boolean searchText2(String text) {
return (this.getRegex() == null) ? true :
// 1. Java Regular Expressione Engine - slower :
//text.matches(this.getRegex());
// 2. faster: precompile regex & use everywhere
// looking exactly match in the string:
//this.pattern.matcher(text).matches();
// find a term contained in the string
this.pattern.matcher(text).find();
}
public void zipFilesJava7() throws IOException {
try (ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(getZipFileName()))){
File baseDir = new File(getPath());
for (File file : zipFiles) {
//fileName must be a relative path
String fileName = getRelativeFileName(file, baseDir);
System.out.println("added to zip fileName : " + fileName);
ZipEntry zipEntry = new ZipEntry(fileName);
zipEntry.setTime(file.lastModified());
out.putNextEntry(zipEntry);
Files.copy(file.toPath(), out); // copy a file to zip
out.closeEntry();
}
}
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
this.pattern = Pattern.compile(regex);
}
public String getZipFileName() {
return zipFileName;
}
public void setZipFileName(String zipFileName) {
this.zipFileName = zipFileName;
}
}