//文件监测类
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.util.Set;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
public class Monitor{
private String path=null;//监控路径
private int ScanTime=0;//扫描时间间隔
private Map FileMap=null;
private TransferFile transfer;
private String[] FileNames=new String[100];
private int count=0;//记录传送文件的数量
private int[] flag=new int[100];
private int n=3;//创建线程的最大数量
public static void main(String args[])
{
Monitor monitor=new Monitor("D:\\temp2",1);
monitor.checkFile();
}
public Monitor(String path,int ScanTime)
{
this.path=path;
this.ScanTime=ScanTime;
this.FileMap=new HashMap();
File file=new File(path);
ScanFile(file,FileMap);//扫描原有目录下的文件
ShowFile();//显示文件夹下的文件内容
}
public void checkFile()
{
File file=new File(path);
ExecutorService exec = Executors.newFixedThreadPool(n);//构建线程池
while(true)
{
try{
Thread.sleep(1000);
Map nowFileMap=new HashMap();
ScanFile(file,nowFileMap);
int f=getAddFileMap(nowFileMap);//获得新增加的文件
if(f==1)
{
for(int i=0;i
{
ThreadMonitor threadMo=new ThreadMonitor(FileNames[i]);
Thread t1 = new Thread(threadMo);
exec.execute(t1);
for(int j=i;j
{
FileNames[j]=FileNames[j+1];
}
count--;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public int getAddFileMap(Map nowFileMap)
{
Iterator iterator=nowFileMap.keySet().iterator();
while(iterator.hasNext())
{
String key=iterator.next();
if(FileMap.get(key)==null)
{
FileNames[count]=key;
count++;
FileMap.put(key, nowFileMap.get(key));
System.out.println("增加:"+nowFileMap.get(key)+count);
}
}
if(count!=0)
{
return 1;
}
return 0;
}
public void ShowFile()
{
System.out.print(path+"文件目录如下:\n");
Set set=FileMap.keySet();
Iterator iterator=set.iterator();//迭代器
while(iterator.hasNext())
{
System.out.println(FileMap.get(iterator.next()));
}
System.out.println("==============================");
}
//扫描文件夹下的所有内容
public void ScanFile(File file,Map oldFileMap)
{
String[] FileList=file.list();
//System.out.print(FileList[0]+"dsd");
if(FileList!=null)//文件列表不为空
{
for(int i=0;i
{
String name=file.getAbsolutePath()+"\\"+FileList[i];
File tempFile=new File(name);
if(tempFile.isDirectory())
{
oldFileMap.put(name, "文件夹 \t"+name);
ScanFile(tempFile,oldFileMap);
}
else
{
oldFileMap.put(name, "文件 \t"+name);
}
}
}
else
{
//System.out.print("目前文件夹中还没有文件!");
}
}
}
class ThreadMonitor implements Runnable{
private String name=null;
private TransferFile transfer;
public ThreadMonitor(String name)
{
this.name=name;
}
public void run()
{
int flag=2;
while(true)
{
try{
Thread.sleep(1000);
transfer=new TransferFile();
flag=transfer.copyFile(this.name,"d:/abc");
if(flag==0)//代表本文件传输完毕
{
break;
}
}
catch(Exception e)
{
e.getStackTrace();
}
}
System.out.print(this.name+":"+Thread.currentThread().getName()+"运行完毕");
}
}
//文件转移类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class TransferFile {
private String oldpath=null;
private String newpath=null;
//将文件拷贝到指定的文件夹下
public int copyFile(String oldpath,String newpath)
{
try{
File oldFile=new File(oldpath);//获取原文件
File newFile=new File(newpath);
if(!newFile.exists())//源文件夹不存在,则创建一个
{
newFile.mkdir();
}
int len=0;
if(oldFile.exists())//如果文件存在
{
FileInputStream inFile=new FileInputStream(oldpath);
FileOutputStream outFile=new FileOutputStream(newpath+"/"+(oldFile.getName()).toString());
byte[] buffer=new byte[1024*10];
while((len=inFile.read(buffer))!=-1)
{
outFile.write(buffer, 0, len);
}
outFile.flush();
outFile.close();
inFile.close();
oldFile.delete();//删除备份后的原文件
}
}catch(Exception e){
return -1;
}
return 0;
}
}