查看完整版本: [-- Java 新建、复制、删除、移动文件(夹)操作类 --]

风信Java论坛 ›› Java 基础开发 ›› Java 新建、复制、删除、移动文件(夹)操作类 登录 -> 注册

1F Java 新建、复制、删除、移动文件(夹)操作类   唧唧 Post by : 2009-06-24 11:11:24.0

以下包含了:新建目录、新建文件、删除文件、删除文件夹、删除文件夹里面的所有文件、复制单个文件、复制整个文件夹内容、移动文件到指定目录、移动文件夹到指定目录的操作

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

 


风信Java论坛 ›› Java 基础开发 ›› Java 新建、复制、删除、移动文件(夹)操作类 登录 -> 注册

查看完整版本: [-- Java 新建、复制、删除、移动文件(夹)操作类 --]
CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 10 ms,0 (Queries)  Gzip enabled
粤ICP备07511478号