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();
        }
    }
  • 相关阅读:
    oracle拼接函数:将多个字段拼接在一行显示
    Source Insight 自定义命令说明
    harview .har文件解析
    GSM设备和网络错误代码
    mknod 创建设备
    linux下的usb抓包方法
    一些函数
    vmware 软件打开 自动开启虚拟机(快捷方式)
    Unix下C程序内存泄漏检测工具Valgrind安装与使用
    windows vmware 系统自启动
  • 原文地址:https://www.cnblogs.com/huangcongcong/p/4003727.html
Copyright © 2011-2022 走看看