内容转自:http://www.dev26.com/blog/article/421

 

 
  
  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.     public void newFolder(String folderPath) {   
  15.         try {   
  16.             String filePath = folderPath;   
  17.             filePath = filePath.toString();   
  18.             File myFilePath = new File(filePath);   
  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.     public void newFile(String filePathAndName, String fileContent) {   
  31.      
  32.         try {   
  33.             String filePath = filePathAndName;   
  34.             filePath = filePath.toString();   
  35.             File myFilePath = new File(filePath);   
  36.             if (!myFilePath.exists()) {   
  37.                 myFilePath.createNewFile();   
  38.             }   
  39.             FileWriter resultFile = new FileWriter(myFilePath);   
  40.             PrintWriter myFile = new PrintWriter(resultFile); 
  41.             String strContent = fileContent;   
  42.             myFile.println(strContent);   
  43.             resultFile.close();   
  44.      
  45.         } catch (Exception e) {   
  46.             System.out.println("新建文件操作出错 ");   
  47.             e.printStackTrace();   
  48.         }   
  49.      
  50.     }   
  51.      
  52.  
  53.     public void delFile(String filePathAndName) {   
  54.         try {   
  55.             String filePath = filePathAndName;   
  56.             filePath = filePath.toString();   
  57.             java.io.File myDelFile = new java.io.File(filePath);   
  58.             myDelFile.delete();   
  59.      
  60.         } catch (Exception e) {   
  61.             System.out.println("删除文件操作出错 ");   
  62.             e.printStackTrace();   
  63.      
  64.         }   
  65.      
  66.     }   
  67.      
  68.   
  69.     public void delFolder(String folderPath) {   
  70.         try {   
  71.             delAllFile(folderPath); // 删除完里面所有内容   
  72.             String filePath = folderPath;   
  73.             filePath = filePath.toString();   
  74.             java.io.File myFilePath = new java.io.File(filePath);   
  75.             myFilePath.delete(); // 删除空文件夹   
  76.      
  77.         } catch (Exception e) {   
  78.             System.out.println("删除文件夹操作出错 ");   
  79.             e.printStackTrace();   
  80.      
  81.         }   
  82.      
  83.     }   
  84.      
  85.    
  86.     public void delAllFile(String path) {   
  87.         File file = new File(path);   
  88.         if (!file.exists()) {   
  89.             return;   
  90.         }   
  91.         if (!file.isDirectory()) {   
  92.             return;   
  93.         }   
  94.         String[] tempList = file.list();   
  95.         File temp = null;   
  96.         for (int i = 0; i < tempList.length; i++) {   
  97.             if (path.endsWith(File.separator)) {   
  98.                 temp = new File(path + tempList[i]);   
  99.             } else {   
  100.                 temp = new File(path + File.separator + tempList[i]);   
  101.             }   
  102.             if (temp.isFile()) {   
  103.                 temp.delete();   
  104.             }   
  105.             if (temp.isDirectory()) {   
  106.                 delAllFile(path + "/ " + tempList[i]);// 先删除文件夹里面的文件   
  107.                 delFolder(path + "/ " + tempList[i]);// 再删除空文件夹   
  108.             }   
  109.         }   
  110.     }   
  111.      
  112.     
  113.     public void copyFile(String oldPath, String newPath) {   
  114.         try {   
  115.             int bytesum = 0;   
  116.             int byteread = 0;   
  117.             File oldfile = new File(oldPath);   
  118.             if (oldfile.exists()) { // 文件存在时   
  119.                 InputStream inStream = new FileInputStream(oldPath); // 读入原文件   
  120.                 FileOutputStream fs = new FileOutputStream(newPath);   
  121.                 byte[] buffer = new byte[1444];   
  122.                 while ((byteread = inStream.read(buffer)) != -1) {   
  123.                     bytesum += byteread; // 字节数 文件大小   
  124.                     System.out.println(bytesum);   
  125.                     fs.write(buffer, 0, byteread);   
  126.                 }   
  127.                 inStream.close();   
  128.             }   
  129.         } catch (Exception e) {   
  130.             System.out.println("复制单个文件操作出错 ");   
  131.             e.printStackTrace();   
  132.      
  133.         }   
  134.      
  135.     }   
  136.  
  137.     public void copyFolder(String oldPath, String newPath) {  
  138.         try {   
  139.             (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹   
  140.             File a = new File(oldPath);   
  141.             String[] file = a.list();   
  142.             File temp = null;   
  143.             for (int i = 0; i < file.length; i++) {   
  144.                 if (oldPath.endsWith(File.separator)) {   
  145.                     temp = new File(oldPath + file[i]);   
  146.                 } else {   
  147.                     temp = new File(oldPath + File.separator + file[i]);   
  148.                 }   
  149.      
  150.                 if (temp.isFile()) {   
  151.                     FileInputStream input = new FileInputStream(temp);   
  152.                     FileOutputStream output = new FileOutputStream(newPath   
  153.                             + "/ " + (temp.getName()).toString());   
  154.                     byte[] b = new byte[1024 * 5];   
  155.                     int len;   
  156.                     while ((len = input.read(b)) != -1) {   
  157.                         output.write(b, 0, len);   
  158.                     }   
  159.                     output.flush();   
  160.                     output.close();   
  161.                     input.close();   
  162.                 }   
  163.                 if (temp.isDirectory()) {// 如果是子文件夹   
  164.                     copyFolder(oldPath + "/ " + file[i], newPath + "/ " 
  165.                             + file[i]);   
  166.                 }   
  167.             }   
  168.         } catch (Exception e) {   
  169.             System.out.println("复制整个文件夹内容操作出错 ");   
  170.             e.printStackTrace();   
  171.      
  172.         }   
  173.      
  174.     }   
  175.  
  176.     public void moveFile(String oldPath, String newPath) {   
  177.         copyFile(oldPath, newPath);   
  178.         delFile(oldPath);   
  179.      
  180.     }   
  181.      
  182.  
  183.     public void moveFolder(String oldPath, String newPath) {  
  184.         copyFolder(oldPath, newPath);   
  185.         delFolder(oldPath);   
  186.     }