java高级8- 复制文件,复制文件夹

1.复制文件

public class TestStream {
    /**
     * 
     * @param srcPath 源文件
     * @param destPath 目标文件
     */
    public static void copyFile(String srcPath, String destPath){
         
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
         
        //缓存区,一次性读取1024字节
        byte[] buffer = new byte[1024];
 
        try (
                FileInputStream fis = new FileInputStream(srcFile);
                FileOutputStream fos = new FileOutputStream(destFile);        		
        ){
            while(true){
                //实际读取的长度是 actuallyReaded,有可能小于1024
                int actuallyReaded = fis.read(buffer);
                //-1表示没有可读的内容了
                if(-1==actuallyReaded)
                    break;
                fos.write(buffer, 0, actuallyReaded);
                fos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
         
    }
     
    /**
     * 
     * @param srcPath 源文件夹
     * @param destPath 目标文件夹
     */
    public static void copyFolder(String srcPath, String destPath){
         
    }
     
    public static void main(String[] args) {
         
        copyFile("d:/lol.txt", "d:/lol2.txt");
        
    }
}

2.复制文件夹 

public class TestStream {
    
    /**
     * 
     * @param srcPath 源文件
     * @param destPath 目标文件
     */
    public static void copyFile(String srcPath, String destPath){
         
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
         
        //缓存区,一次性读取1024字节
        byte[] buffer = new byte[1024];
 
        try (
                FileInputStream fis = new FileInputStream(srcFile);
                FileOutputStream fos = new FileOutputStream(destFile);
        ){
            while(true){
                //实际读取的长度是 actuallyReaded,有可能小于1024
                int actuallyReaded = fis.read(buffer);
                //-1表示没有可读的内容了
                if(-1==actuallyReaded)
                    break;
                fos.write(buffer, 0, actuallyReaded);
                fos.flush();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
    }
     
    /**
     * 
     * @param srcPath 源文件夹
     * @param destPath 目标文件夹
     */
    public static void copyFolder(String srcPath, String destPath){
         
        File srcFolder = new File(srcPath);
        File destFolder = new File(destPath);
        //源文件夹不存在
        if(!srcFolder.exists())
            return;
        //源文件夹不是一个文件夹
        if(!srcFolder.isDirectory())
            return;
        //目标文件夹是一个文件
        if(destFolder.isFile())
            return;
        //目标文件夹不存在
        if(!destFolder.exists())
            destFolder.mkdirs();

        //遍历源文件夹
        File[] files=  srcFolder.listFiles();
        for (File srcFile : files) {
        	//如果是文件,就复制
            if(srcFile.isFile()){
                File newDestFile = new File(destFolder,srcFile.getName());
                copyFile(srcFile.getAbsolutePath(), newDestFile.getAbsolutePath());
            }
        	//如果是文件夹,就递归
            if(srcFile.isDirectory()){
                File newDestFolder = new File(destFolder,srcFile.getName());
                copyFolder(srcFile.getAbsolutePath(),newDestFolder.getAbsolutePath());
            }
        }
    }
    public static void main(String[] args) {
        copyFolder("d:/LOLFolder", "d:/LOLFolder2");
    }
}

3.查找文件内容

假设你的项目目录是 e:/project,遍历这个目录下所有的java文件(包括子文件夹),找出文件内容包括 Magic的那些文件,并打印出来。

public class TestStream {

	/**
	 * @param file 查找的目录
	 * @param search 查找的字符串
	 */
	public static void search(File file, String search) {
		if (file.isFile()) {
			if(file.getName().toLowerCase().endsWith(".java")){
				String fileContent = readFileConent(file);
				if(fileContent.contains(search)){
					System.out.printf("找到子目标字符串%s,在文件:%s%n",search,file);
				}
			}
		}
		if (file.isDirectory()) {
			File[] fs = file.listFiles();
			for (File f : fs) {
				search(f, search);
			}
		}
	}
	
	public static String readFileConent(File file){
        try (FileReader fr = new FileReader(file)) {
            char[] all = new char[(int) file.length()];
            fr.read(all);
            return new String(all);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

	}

	public static void main(String[] args) {
		File folder =new File("e:\\project");
		search(folder,"Magic");
	}
}

 

你可能感兴趣的:(JAVA)