Java新建目录、新建文件、删除文件、删除文件夹、删除文件夹里面的所有文件、复制整个文件夹内容

  1. package com.zzh.base;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.FileWriter;  
  7. import java.io.InputStream;  
  8. import java.io.PrintWriter;  
  9.   
  10. public  class  NewDelCopyFile  {    
  11.    public  NewDelCopyFile()  {    
  12.    }    
  13.    
  14.    /**   
  15.      *  新建目录   
  16.      *  @param  folderPath  String  如  c:/fqf   
  17.      *  @return  boolean   
  18.      */    
  19.    public  void  newFolder(String  folderPath)  {    
  20.        try  {    
  21.            String  filePath  =  folderPath;    
  22.            filePath  =  filePath.toString();    
  23.            java.io.File  myFilePath  =  new  java.io.File(filePath);    
  24.            if  (!myFilePath.exists())  {    
  25.                myFilePath.mkdir();    
  26.            }    
  27.        }    
  28.        catch  (Exception  e)  {    
  29.            System.out.println("新建目录操作出错");    
  30.            e.printStackTrace();    
  31.        }    
  32.    }    
  33.    
  34.    /**   
  35.      *  新建文件   
  36.      *  @param  filePathAndName  String  文件路径及名称  如c:/fqf.txt   
  37.      *  @param  fileContent  String  文件内容   
  38.      *  @return  boolean   
  39.      */    
  40.    public  void  newFile(String  filePathAndName,  String  fileContent)  {    
  41.    
  42.        try  {    
  43.            String  filePath  =  filePathAndName;    
  44.            filePath  =  filePath.toString();    
  45.            File  myFilePath  =  new  File(filePath);    
  46.            if  (!myFilePath.exists())  {    
  47.                myFilePath.createNewFile();    
  48.            }    
  49.            FileWriter  resultFile  =  new  FileWriter(myFilePath);    
  50.            PrintWriter  myFile  =  new  PrintWriter(resultFile);    
  51.            String  strContent  =  fileContent;    
  52.            myFile.println(strContent);    
  53.            resultFile.close();    
  54.    
  55.        }    
  56.        catch  (Exception  e)  {    
  57.            System.out.println("新建文件操作出错");    
  58.            e.printStackTrace();    
  59.    
  60.        }    
  61.    
  62.    }    
  63.    
  64.    /**   
  65.      *  删除文件   
  66.      *  @param  filePathAndName  String  文件路径及名称  如c:/fqf.txt   
  67.      *  @param  fileContent  String   
  68.      *  @return  boolean   
  69.      */    
  70.    public  void  delFile(String  filePathAndName)  {    
  71.        try  {    
  72.            String  filePath  =  filePathAndName;    
  73.            filePath  =  filePath.toString();    
  74.            java.io.File  myDelFile  =  new  java.io.File(filePath);    
  75.            myDelFile.delete();    
  76.    
  77.        }    
  78.        catch  (Exception  e)  {    
  79.            System.out.println("删除文件操作出错");    
  80.            e.printStackTrace();    
  81.    
  82.        }    
  83.    
  84.    }    
  85.    
  86.    /**  
  87.      *  删除文件夹   
  88.      *  @param  filePathAndName  String  文件夹路径及名称  如c:/fqf   
  89.      *  @param  fileContent  String   
  90.      *  @return  boolean   
  91.      */    
  92.    public  void  delFolder(String  folderPath)  {    
  93.        try  {    
  94.            delAllFile(folderPath);  //删除完里面所有内容    
  95.            String  filePath  =  folderPath;    
  96.            filePath  =  filePath.toString();    
  97.            java.io.File  myFilePath  =  new  java.io.File(filePath);    
  98.            myFilePath.delete();  //删除空文件夹    
  99.    
  100.        }    
  101.        catch  (Exception  e)  {    
  102.            System.out.println("删除文件夹操作出错");    
  103.            e.printStackTrace();    
  104.    
  105.        }    
  106.    
  107.    }    
  108.    
  109.    /**    
  110.      *  删除文件夹里面的所有文件   
  111.      *  @param  path  String  文件夹路径  如  c:/fqf   
  112.      */    
  113.    public  void  delAllFile(String  path)  {    
  114.        File  file  =  new  File(path);    
  115.        if  (!file.exists())  {    
  116.            return;    
  117.        }    
  118.        if  (!file.isDirectory())  {    
  119.            return;    
  120.        }    
  121.        String[]  tempList  =  file.list();    
  122.        File  temp  =  null;    
  123.        for  (int  i  =  0;  i  <  tempList.length;  i++)  {    
  124.            if  (path.endsWith(File.separator))  {    
  125.                temp  =  new  File(path  +  tempList[i]);    
  126.            }    
  127.            else  {    
  128.                temp  =  new  File(path  +  File.separator  +  tempList[i]);    
  129.            }    
  130.            if  (temp.isFile())  {    
  131.                temp.delete();    
  132.            }    
  133.            if  (temp.isDirectory())  {    
  134.                delAllFile(path+"/"+  tempList[i]);//先删除文件夹里面的文件    
  135.                delFolder(path+"/"+  tempList[i]);//再删除空文件夹    
  136.            }    
  137.        }    
  138.    }    
  139.    
  140.    /**  
  141.      *  复制单个文件   
  142.      *  @param  oldPath  String  原文件路径  如:c:/fqf.txt   
  143.      *  @param  newPath  String  复制后路径  如:f:/fqf.txt   
  144.      *  @return  boolean   
  145.      */    
  146.    public  void  copyFile(String  oldPath,  String  newPath)  {    
  147.        try  {    
  148.            int  bytesum  =  0;    
  149.            int  byteread  =  0;    
  150.            File  oldfile  =  new  File(oldPath);    
  151.            if  (oldfile.exists())  {  //文件存在时    
  152.                InputStream  inStream  =  new  FileInputStream(oldPath);  //读入原文件    
  153.                FileOutputStream  fs  =  new  FileOutputStream(newPath);    
  154.                byte[]  buffer  =  new  byte[1444];    
  155.                int  length;    
  156.                while  (  (byteread  =  inStream.read(buffer))  !=  -1)  {    
  157.                    bytesum  +=  byteread;  //字节数  文件大小    
  158.                    System.out.println(bytesum);    
  159.                    fs.write(buffer,  0,  byteread);    
  160.                }    
  161.                inStream.close();    
  162.            }    
  163.        }    
  164.        catch  (Exception  e)  {    
  165.            System.out.println("复制单个文件操作出错");    
  166.            e.printStackTrace();    
  167.    
  168.        }    
  169.    
  170.    }    
  171.    
  172.    /**    
  173.      *  复制整个文件夹内容   
  174.      *  @param  oldPath  String  原文件路径  如:c:/fqf   
  175.      *  @param  newPath  String  复制后路径  如:f:/fqf/ff   
  176.      *  @return  boolean   
  177.      */    
  178.    public  void  copyFolder(String  oldPath,  String  newPath)  {    
  179.    
  180.        try  {    
  181.            (new  File(newPath)).mkdirs();  //如果文件夹不存在  则建立新文件夹    
  182.            File  a=new  File(oldPath);    
  183.            String[]  file=a.list();    
  184.            File  temp=null;    
  185.            for  (int  i  =  0;  i  <  file.length;  i++)  {    
  186.                if(oldPath.endsWith(File.separator)){    
  187.                    temp=new  File(oldPath+file[i]);    
  188.                }    
  189.                else{    
  190.                    temp=new  File(oldPath+File.separator+file[i]);    
  191.                }    
  192.    
  193.                if(temp.isFile()){    
  194.                    FileInputStream  input  =  new  FileInputStream(temp);    
  195.                    FileOutputStream  output  =  new  FileOutputStream(newPath  +  "/"  +    
  196.                            (temp.getName()).toString());    
  197.                    byte[]  b  =  new  byte[1024  *  5];    
  198.                    int  len;    
  199.                    while  (  (len  =  input.read(b))  !=  -1)  {    
  200.                        output.write(b,  0,  len);    
  201.                    }    
  202.                    output.flush();    
  203.                    output.close();    
  204.                    input.close();    
  205.                }    
  206.                if(temp.isDirectory()){//如果是子文件夹    
  207.                    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);    
  208.                }    
  209.            }    
  210.        }    
  211.        catch  (Exception  e)  {    
  212.            System.out.println("复制整个文件夹内容操作出错");    
  213.            e.printStackTrace();    
  214.    
  215.        }    
  216.    
  217.    }    
  218.    
  219.    /**   
  220.      *  移动文件到指定目录   
  221.      *  @param  oldPath  String  如:c:/fqf.txt   
  222.      *  @param  newPath  String  如:d:/fqf.txt   
  223.      */    
  224.    public  void  moveFile(String  oldPath,  String  newPath)  {    
  225.        copyFile(oldPath,  newPath);    
  226.        delFile(oldPath);    
  227.    
  228.    }    
  229.    
  230.    /**   
  231.      *  移动文件到指定目录   
  232.      *  @param  oldPath  String  如:c:/fqf.txt   
  233.      *  @param  newPath  String  如:d:/fqf.txt   
  234.      */    
  235.    public  void  moveFolder(String  oldPath,  String  newPath)  {    
  236.        copyFolder(oldPath,  newPath);    
  237.        delFolder(oldPath);    
  238.    
  239.    }    
  240.    public static void main(String[] args) {  
  241.        NewDelCopyFile file = new NewDelCopyFile();  
  242.     //file.newFolder("D:/zhou.txt");  
  243.     //file.newFile("D:/zhou.txt","fdsfdsfdsfs");  
  244.     file.delFolder("D:/zhouzehui.txt");  
  245.     file.delFile("D:/test.txt");  
  246.    }  
  247. // 拷贝文件  
  248.    private void copyFile2(String source, String dest) {  
  249.    try {  
  250.    File in = new File(source);  
  251.    File out = new File(dest);  
  252.    FileInputStream inFile = new FileInputStream(in);  
  253.    FileOutputStream outFile = new FileOutputStream(out);  
  254.    byte[] buffer = new byte[1024];  
  255.    int i = 0;  
  256.    while ((i = inFile.read(buffer)) != -1) {  
  257.    outFile.write(buffer, 0, i);  
  258.    }//end while  
  259.    inFile.close();  
  260.    outFile.close();  
  261.    }//end try  
  262.    catch (Exception e) {  
  263.   
  264.    }//end catch  
  265.    }//end copyFile  
  266.   
  267. }  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值