java

java 删除文件夹、复制文件夹

2019-07-25  本文已影响0人  岁月静好忄

java递归删除文件夹、复制文件夹,联合起来就是资源同步

public class ResourceSyn 
{
    
    private String sourcePath;
    private String newPath;
    public static void main(String[] args) 
    {
        ResourceSyn rSyn = new ResourceSyn();
        //资源基础地址
        rSyn.sourcePath = "xxxxxxxxxxxxx";
        
        //需要被同步资源的地基础址
        rSyn.newPath = "xxxxxxxxxxxx";
        
        long start = System.currentTimeMillis();
                //删除文件
        rSyn.deleteFile(rSyn.newPath+"\\Autoload");
        rSyn.deleteFile(rSyn.newPath+"\\CoreData");
        rSyn.deleteFile(rSyn.newPath+"\\Data");
                //复制文件
        rSyn.copy(rSyn.sourcePath+"\\Autoload", rSyn.newPath+"\\Autoload");
        rSyn.copy(rSyn.sourcePath+"\\CoreData", rSyn.newPath+"\\CoreData");
        rSyn.copy(rSyn.sourcePath+"\\Data", rSyn.newPath+"\\Data");
        System.out.println((System.currentTimeMillis()-start)+"ms");
    }
    
    /**
     * 文件删除
     * @param filePath
     * @return
     */
    public boolean deleteFile(String filePath) 
    {
        
        File dirFile = new File(filePath);
        // 如果dir对应的文件不存在,则退出
        if (dirFile.exists()==false) 
        {
            return false;
        }

        if (dirFile.isFile()) 
        {
            return dirFile.delete();
        } else 
        {

            for (File file : dirFile.listFiles()) 
            {
                deleteFile(file.getAbsolutePath());
            }
        }

        return dirFile.delete();
    }

    
    /**
     * 文件复制
     * @param sourcePath
     * @param newPath
     */
    private void copy(String sourcePath, String newPath)
    {
        File file = new File(sourcePath);
        if(file.isDirectory())
        {
            File newFile = new File(newPath);
            if(newFile.exists()==false)
            {
                newFile.mkdirs();
            }
            String[] fileArray = file.list();
            for(int i=0;i<fileArray.length;i++)
            {
                copy(sourcePath + "\\" + fileArray[i],newPath+"\\"+fileArray[i]);
            }
        }else
        {
            copyFile(sourcePath,newPath);
        }
    }
    
    /**
     * 文件写入
     * @param oldPath
     * @param newPath
     */
    public void copyFile(String oldPath, String newPath) 
    {
          
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = null;
        FileOutputStream out = null;
        try 
        {
            in = new FileInputStream(oldFile);
            out = new FileOutputStream(file);
            byte[] buffer=new byte[1048576];
            int length=0;
            while((length=in.read(buffer)) != -1)
            {
                out.write(buffer,0,length);
            }   
            
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        } 
        finally
        {
            try 
            {
                out.close();
                in.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
                
            }
          }
      }
}
上一篇下一篇

猜你喜欢

热点阅读