DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page code to monitor new files in folder with file name in the arraylist:
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Apr 2008
Default 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;
}

}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set up my eclipse project to build a jar file from two jar files powah Java 0 11-13-2008 03:55 PM
Editing .dox files with HTML code in Eclipse? dangerdg Eclipse 0 05-22-2008 11:48 AM
Need plugin to monitor log file on remote server dwschulze Eclipse 0 03-19-2008 11:15 AM
code to get the location of file in package explorer jayjamba Eclipse 1 03-18-2008 03:04 AM
Applet to upload files and folder over HTTP jfileupload Java 2 03-11-2008 05:29 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com