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();
    }
}

 

posted @ 2014-10-02 00:00  凡尘里的一根葱  阅读(893)  评论(0编辑  收藏  举报