java 流 复制,重命名,删除目录

package 流;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class 流 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        delDir("d://MyDrivers");
    }

    public static void copyFile(String src,String dest){
        try {
            InputStream inputStream=new FileInputStream(src);
            OutputStream outputStream=new FileOutputStream(dest);
            
            File file=new File(dest);
            if(!file.exists())
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            
            int ch;
            
            try {
                while((ch=inputStream.read())!=-1)
                {
                    outputStream.write(ch);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void renameFile(String path,String oldname,String newname){
        
        if(!oldname.equals(newname)){
            File oldFile=new File(path+"/"+oldname);
            File newFile=new File(path+"/"+newname);
            
            if(newFile.exists())
            {
                System.out.println("该文件已经存在");
            }
            else
            {
                oldFile.renameTo(newFile);
            }
        }
    }
    
    public static void delDir(String path){
        
        File dir=new File(path);
        
        if(dir.exists()){
            File[] tmp=dir.listFiles();
            
            for(int i=0; i<tmp.length; i++)
            {
                if(tmp[i].isDirectory())
                {
                    delDir(path+"/"+tmp[i].getName());
                }
                else
                {
                    tmp[i].delete();
                }
            }
        }
        dir.delete();
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。