|
code to monitor new files in folder with file name in the arraylist: -
04-04-2008, 08:26 AM
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileTest
implements Runnable {
static int fileCount;
static Thread t;
static File[] existingFiles;
static int counterCheck = 0;
List list;
public static void main(String[] args) {
FileTest test = new FileTest();
t = new Thread(test);
t.start();
}
public void run() {
int currentFilecount = 0;
File f = new File("test");
boolean status = f.isDirectory();
File[] currentFiles = null;
if (status) {
currentFilecount = f.listFiles().length;
if (counterCheck == 0) {
existingFiles = f.listFiles();
counterCheck++;
}
} else {
f.mkdir();
}
if (fileCount == currentFilecount) {
System.out.println("No new file");
} else {
System.out.println("No. of new files : "
+ (currentFilecount - fileCount));
currentFiles = f.listFiles();
this.list = compaireArray(existingFiles, currentFiles);
fileCount = currentFilecount;
existingFiles = currentFiles;
}
try {
t.sleep(7000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.run();
}
/**
* compares two array and returns the new files in a new file array !
* @param existingFiles2
* @param currentFiles
* @return
*/
private List compaireArray(File[] existingFiles2, File[] currentFiles) {
List list = new ArrayList();
int counter = 0;
for (int i = 0; i < currentFiles.length; i++) {
for (int j = i; j < existingFiles2.length; j++) {
if (currentFiles[i].getName().equals(
existingFiles2[j].getName())) {
break;
} else {
list.add(currentFiles[j].getName());
counter++;
}
}
}
if (list != null) {
for (int i = 0; i < list.size(); i++) {
System.out.println((String) list.get(i));
}
}
return list;
}
/**
* returns the newly added files
* @return
*/
public List getNewFile() {
return this.list;
}
}
|