FileOperate.java源代码

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值