Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DEVELOP A SYSTEM THAT MONITORS A FILE DIRECTORY LIKE C :/ MY DOCUMENTS IF THIS DIRECTORY WILL HAVE SOME CHANGES OR ADDITIONAL FILES ARE ADDED... AN AUTOMATIC ALERT WILL BE INCORPORATED VIA A SMS...

NEED THIS CODE TO BE DEVELOPED SO THAT SMS SENDING( INCORPORATE AT COMMANDS WITH IT) WILL BE INCLUDED...





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;
}

}
Posted

1 solution

This looks like you posted the text of an assignment, and then your attempt to answer at least part of it. I don't see a question here, although I know you've asked this once before, and been answered. Perhaps you should not ask the same thing over and over, but edit your post instead ? Perhaps you should try the stuff you were told last time you asked ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900