zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    Static了解和复习继承。
    复习篇1.对象和封装
    第一章笔记
    A + B Problem II
    Number Sequence
    Fibonacci Again
    8615 快乐
    8635 气球
    大牛之路II
    8617 阶乘数字和
  • 原文地址:https://www.cnblogs.com/huangcongcong/p/4003727.html
Copyright © 2011-2022 走看看