FileUtil


  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.util.Date;  
  7.   
  8. /** 
  9.  * 功能:该类用于操作文件和文件夹 
  10.  * 作者:邵江 
  11.  * 时间:2011.7.21 
  12.  * 版本:0.1 
  13.  */  
  14. public class FileUtil {  
  15.       
  16.     /** 
  17.      * 将源文件的数据写入到目标文件中, 
  18.      * 不会检查源文件是否存在, 
  19.      * 若目标文件存在则直接写入, 
  20.      * 否则创建目标文件后再进行写入。 
  21.      * @param srcPath 
  22.      * @param desPath 
  23.      */  
  24.     private static void copyFile(String srcPath,String desPath){  
  25.         try {  
  26.             FileInputStream in = new FileInputStream(srcPath);  
  27.             FileOutputStream out = new FileOutputStream(desPath);  
  28.             byte[] bt = new byte[1024];  
  29.             int count;  
  30.             while ((count = in.read(bt)) > 0) {  
  31.                 out.write(bt, 0, count);  
  32.             }  
  33.             in.close();  
  34.             out.close();   
  35.         } catch (IOException ex) {  
  36.             ex.printStackTrace();  
  37.         }         
  38.     }  
  39.       
  40.     /** 
  41.      * 复制文件,若文件存在则替换该文件。 
  42.      * @param srcPath 
  43.      * @param desPath 
  44.      * @throws Exception 
  45.      */  
  46.     public static void copyAndReplaceFile(String srcPath,String desPath) throws Exception{  
  47.         srcPath = separatorReplace(srcPath);  
  48.         desPath = separatorReplace(desPath);  
  49.         File srcFile = new File(srcPath);  
  50.         if(!srcFile.isFile()){  
  51.             throw new Exception("source file not found!");  
  52.         }  
  53.         copyFile(srcPath,desPath);  
  54.     }  
  55.       
  56.     /** 
  57.      * 复制文件,若文件已存在则不进行替换。 
  58.      * @param srcPath 
  59.      * @param desPath 
  60.      * @throws Exception 
  61.      */  
  62.     public static void copyAndNotReplaceFile(String srcPath,String desPath) throws Exception{  
  63.         srcPath = separatorReplace(srcPath);  
  64.         desPath = separatorReplace(desPath);  
  65.         File srcFile = new File(srcPath);  
  66.         File desFile = new File(desPath);  
  67.         if(!srcFile.isFile()){  
  68.             throw new Exception("source file not found!");  
  69.         }  
  70.         if(desFile.isFile()){  
  71.             return;  
  72.         }  
  73.         copyFile(srcPath,desPath);  
  74.     }  
  75.       
  76.     /** 
  77.      * 移动文件,若文件存在则替换该文件。 
  78.      * @param srcPath 
  79.      * @param desPath 
  80.      * @throws Exception 
  81.      */  
  82.     public static void moveAndReplaceFile(String srcPath,String desPath) throws Exception{  
  83.         srcPath = separatorReplace(srcPath);  
  84.         desPath = separatorReplace(desPath);  
  85.         copyAndReplaceFile(srcPath,desPath);  
  86.         deleteFile(srcPath);  
  87.     }  
  88.       
  89.     /** 
  90.      * 移动文件,若文件存在则不进行替换。 
  91.      * @param srcPath 
  92.      * @param desPath 
  93.      * @throws Exception 
  94.      */  
  95.     public static void moveAndNotReplaceFile(String srcPath,String desPath) throws Exception{  
  96.         srcPath = separatorReplace(srcPath);  
  97.         desPath = separatorReplace(desPath);  
  98.         copyAndNotReplaceFile(srcPath,desPath);  
  99.         deleteFile(srcPath);  
  100.     }  
  101.       
  102.     /** 
  103.      * 复制并合并文件夹, 
  104.      * 不会替换目标文件夹中已经存在的文件或文件夹。 
  105.      * @param srcPath 
  106.      * @param desPath 
  107.      * @throws Exception  
  108.      */  
  109.     public static void copyAndMergerFolder(String srcPath,String desPath) throws Exception{       
  110.         srcPath = separatorReplace(srcPath);  
  111.         desPath = separatorReplace(desPath);  
  112.         File folder = getFolder(srcPath);  
  113.         createFolder(desPath);  
  114.         File[] files = folder.listFiles();  
  115.         for(File file:files){  
  116.             String src = file.getAbsolutePath();  
  117.             String des = desPath+File.separator+file.getName();  
  118.             if(file.isFile()){  
  119.                 copyAndNotReplaceFile(src,des);  
  120.             }else if(file.isDirectory()){  
  121.                 copyAndMergerFolder(src,des);  
  122.             }  
  123.         }  
  124.     }  
  125.       
  126.     /** 
  127.      * 复制并替换文件夹, 
  128.      * 将目标文件夹完全替换成源文件夹, 
  129.      * 目标文件夹原有的资料会丢失。 
  130.      * @param srcPath 
  131.      * @param desPath 
  132.      * @throws Exception  
  133.      */  
  134.     public static void copyAndReplaceFolder(String srcPath,String desPath) throws Exception{     
  135.         srcPath = separatorReplace(srcPath);  
  136.         desPath = separatorReplace(desPath);  
  137.         File folder = getFolder(srcPath);  
  138.         createNewFolder(desPath);  
  139.         File[] files = folder.listFiles();  
  140.         for(File file:files){  
  141.             String src = file.getAbsolutePath();  
  142.             String des = desPath+File.separator+file.getName();  
  143.             if(file.isFile()){  
  144.                 copyAndReplaceFile(src,des);  
  145.             }else if(file.isDirectory()){  
  146.                 copyAndReplaceFolder(src,des);  
  147.             }  
  148.         }  
  149.     }   
  150.       
  151.     /** 
  152.      * 合并文件夹后,将源文件夹删除。 
  153.      * @param srcPath 
  154.      * @param desPath 
  155.      * @throws Exception  
  156.      */  
  157.     public static void moveAndMergerFolder(String srcPath,String desPath) throws Exception{  
  158.         srcPath = separatorReplace(srcPath);  
  159.         desPath = separatorReplace(desPath);          
  160.         copyAndMergerFolder(srcPath,desPath);  
  161.         deleteFolder(srcPath);  
  162.     }  
  163.       
  164.     /** 
  165.      * 替换文件夹后,将源文件夹删除。 
  166.      * @param srcPath 
  167.      * @param desPath 
  168.      * @throws Exception  
  169.      */  
  170.     public static void moveAndReplaceFolder(String srcPath,String desPath) throws Exception{  
  171.         srcPath = separatorReplace(srcPath);  
  172.         desPath = separatorReplace(desPath);  
  173.         copyAndReplaceFolder(srcPath,desPath);  
  174.         deleteFolder(srcPath);  
  175.     }      
  176.   
  177.     /** 
  178.      * 创建文件夹,如果文件夹存在则不进行创建。 
  179.      * @param path 
  180.      * @throws Exception  
  181.      */  
  182.     public static void createFolder(String path) throws Exception{  
  183.         path = separatorReplace(path);  
  184.         File folder = new File(path);  
  185.         if(folder.isDirectory()){  
  186.             return;  
  187.         }else if(folder.isFile()){  
  188.             deleteFile(path);  
  189.         }  
  190.         folder.mkdirs();  
  191.     }  
  192.       
  193.     /** 
  194.      * 创建一个新的文件夹,如果文件夹存在,则删除后再创建。 
  195.      * @param path 
  196.      * @throws Exception 
  197.      */  
  198.     public static void createNewFolder(String path) throws Exception{  
  199.         path = separatorReplace(path);  
  200.         File folder = new File(path);  
  201.         if(folder.isDirectory()){  
  202.             deleteFolder(path);  
  203.         }else if(folder.isFile()){  
  204.             deleteFile(path);  
  205.         }  
  206.         folder.mkdirs();  
  207.     }  
  208.       
  209.     /** 
  210.      * 创建一个文件,如果文件存在则不进行创建。 
  211.      * @param path 
  212.      * @throws Exception 
  213.      */  
  214.     public static File createFile(String path) throws Exception{  
  215.         path = separatorReplace(path);  
  216.         File file = new File(path);  
  217.         if(file.isFile()){  
  218.             return file;  
  219.         }else if(file.isDirectory()){  
  220.             deleteFolder(path);  
  221.         }  
  222.         return createFile(file);  
  223.     }  
  224.       
  225.     /** 
  226.      * 创建一个新文件,如果存在同名的文件或文件夹将会删除该文件或文件夹, 
  227.      * 如果父目录不存在则创建父目录。 
  228.      * @param path 
  229.      * @throws Exception 
  230.      */  
  231.     public static File createNewFile(String path) throws Exception{  
  232.         path = separatorReplace(path);  
  233.         File file = new File(path);  
  234.         if(file.isFile()){  
  235.             deleteFile(path);  
  236.         }else if(file.isDirectory()){  
  237.             deleteFolder(path);  
  238.         }  
  239.         return createFile(file);  
  240.     }  
  241.       
  242.     /** 
  243.      * 分隔符替换 
  244.      * window下测试通过 
  245.      * @param path 
  246.      * @return 
  247.      */  
  248.     public static String separatorReplace(String path){  
  249.         return path.replace("\\","/");  
  250.     }  
  251.       
  252.     /** 
  253.      * 创建文件及其父目录。 
  254.      * @param file 
  255.      * @throws Exception 
  256.      */  
  257.     public static File createFile(File file) throws Exception{  
  258.         createParentFolder(file);  
  259.         if(!file.createNewFile()) {  
  260.             throw new Exception("create file failure!");  
  261.         }  
  262.         return file;  
  263.     }  
  264.       
  265.     /** 
  266.      * 创建父目录 
  267.      * @param file 
  268.      * @throws Exception 
  269.      */  
  270.     private static void createParentFolder(File file) throws Exception{  
  271.         if(!file.getParentFile().exists()) {  
  272.             if(!file.getParentFile().mkdirs()) {  
  273.                 throw new Exception("create parent directory failure!");  
  274.             }  
  275.         }  
  276.     }  
  277.       
  278.     /** 
  279.      * 根据文件路径删除文件,如果路径指向的文件不存在或删除失败则抛出异常。 
  280.      * @param path 
  281.      * @return 
  282.      * @throws Exception  
  283.      */  
  284.     public static void deleteFile(String path) throws Exception {  
  285.         path = separatorReplace(path);  
  286.         File file = getFile(path);      
  287.         if(!file.delete()){  
  288.             throw new Exception("delete file failure");  
  289.         }                        
  290.     }  
  291.       
  292.     /** 
  293.      * 删除指定目录中指定前缀和后缀的文件。 
  294.      * @param dir 
  295.      * @param prefix 
  296.      * @param suffix 
  297.      * @throws Exception  
  298.      */  
  299.     public static void deleteFile(String dir,String prefix,String suffix) throws Exception{       
  300.         dir = separatorReplace(dir);  
  301.         File directory = getFolder(dir);  
  302.         File[] files = directory.listFiles();  
  303.         for(File file:files){  
  304.             if(file.isFile()){  
  305.                 String fileName = file.getName();  
  306.                 if(fileName.startsWith(prefix)&&fileName.endsWith(suffix)){  
  307.                     deleteFile(file.getAbsolutePath());  
  308.                 }  
  309.             }  
  310.         }      
  311.     }  
  312.       
  313.     /** 
  314.      * 根据路径删除文件夹,如果路径指向的目录不存在则抛出异常, 
  315.      * 若存在则先遍历删除子项目后再删除文件夹本身。 
  316.      * @param path 
  317.      * @throws Exception  
  318.      */  
  319.     public static void deleteFolder(String path) throws Exception {  
  320.         path = separatorReplace(path);  
  321.         File folder = getFolder(path);  
  322.         File[] files = folder.listFiles();   
  323.         for(File file:files) {                  
  324.             if(file.isDirectory()){  
  325.                 deleteFolder(file.getAbsolutePath());  
  326.             }else if(file.isFile()){                      
  327.                 deleteFile(file.getAbsolutePath());                                   
  328.             }  
  329.         }    
  330.         folder.delete();   
  331.     }  
  332.       
  333.     /** 
  334.      * 查找目标文件夹下的目标文件 
  335.      * @param dir 
  336.      * @param fileName 
  337.      * @return 
  338.      * @throws FileNotFoundException 
  339.      */  
  340.     public static File searchFile(String dir,String fileName) throws FileNotFoundException{  
  341.         dir = separatorReplace(dir);  
  342.         File f = null;  
  343.         File folder = getFolder(dir);  
  344.         File[] files = folder.listFiles();   
  345.         for(File file:files) {                  
  346.             if(file.isDirectory()){  
  347.                 f =  searchFile(file.getAbsolutePath(),fileName);  
  348.                 if(f!=null){  
  349.                     break;  
  350.                 }  
  351.             }else if(file.isFile()){   
  352.                 if(file.getName().equals(fileName)){  
  353.                     f = file;  
  354.                     break;  
  355.                 }                                                             
  356.             }  
  357.         }            
  358.         return f;  
  359.     }  
  360.             
  361.     /** 
  362.      * 获得文件类型。 
  363.      * @param path:文件路径 
  364.      * @return 
  365.      * @throws FileNotFoundException  
  366.      */  
  367.     public static String getFileType(String path) throws FileNotFoundException {  
  368.         path = separatorReplace(path);  
  369.         File file = getFile(path);  
  370.         String fileName = file.getName();  
  371.         String[] strs = fileName.split("\\.");  
  372.         if(strs.length<2){  
  373.             return "unknownType";  
  374.         }  
  375.         return strs[strs.length-1];  
  376.     }  
  377.       
  378.     /** 
  379.      * 根据文件路径,获得该路径指向的文件的大小。 
  380.      * @param path 
  381.      * @return 
  382.      * @throws FileNotFoundException 
  383.      */  
  384.     public static long getFileSize(String path) throws FileNotFoundException{  
  385.         path = separatorReplace(path);        
  386.         File file = getFile(path);  
  387.         return file.length();  
  388.     }  
  389.       
  390.     /** 
  391.      * 根据文件夹路径,获得该路径指向的文件夹的大小。 
  392.      * 遍历该文件夹及其子目录的文件,将这些文件的大小进行累加,得出的就是文件夹的大小。 
  393.      * @param path 
  394.      * @return 
  395.      * @throws FileNotFoundException 
  396.      */  
  397.     public static long getFolderSize(String path) throws FileNotFoundException{  
  398.         path = separatorReplace(path);                
  399.         long size = 0;  
  400.         File folder = getFolder(path);  
  401.         File[] files = folder.listFiles();  
  402.         for(File file:files){  
  403.             if(file.isDirectory()){  
  404.                 size += getFolderSize(file.getAbsolutePath());  
  405.             }else if(file.isFile()){  
  406.                 size += file.length();  
  407.             }  
  408.         }  
  409.         return size;  
  410.     }  
  411.       
  412.     /** 
  413.      * 通过路径获得文件, 
  414.      * 若不存在则抛异常, 
  415.      * 若存在则返回该文件。 
  416.      * @param path 
  417.      * @return 
  418.      * @throws FileNotFoundException 
  419.      */  
  420.     public static File getFile(String path) throws FileNotFoundException{  
  421.         path = separatorReplace(path);                
  422.         File file = new File(path);  
  423.         if(!file.isFile()){  
  424.             throw new FileNotFoundException("file not found!");  
  425.         }  
  426.         return file;  
  427.     }  
  428.       
  429.     /** 
  430.      * 通过路径获得文件夹, 
  431.      * 若不存在则抛异常, 
  432.      * 若存在则返回该文件夹。 
  433.      * @param path 
  434.      * @return 
  435.      * @throws FileNotFoundException 
  436.      */  
  437.     public static File getFolder(String path) throws FileNotFoundException{  
  438.         path = separatorReplace(path);                
  439.         File folder = new File(path);  
  440.         if(!folder.isDirectory()){  
  441.             throw new FileNotFoundException("folder not found!");  
  442.         }  
  443.         return folder;  
  444.     }  
  445.       
  446.     /** 
  447.      * 获得文件最后更改时间。 
  448.      * @param path 
  449.      * @return 
  450.      * @throws FileNotFoundException 
  451.      */  
  452.     public static Date getFileLastModified(String path) throws FileNotFoundException{  
  453.         path = separatorReplace(path);                
  454.         File file = getFile(path);  
  455.         return new Date(file.lastModified());  
  456.     }  
  457.       
  458.     /** 
  459.      * 获得文件夹最后更改时间。 
  460.      * @param path 
  461.      * @return 
  462.      * @throws FileNotFoundException 
  463.      */  
  464.     public static Date getFolderLastModified(String path) throws FileNotFoundException{  
  465.         path = separatorReplace(path);        
  466.         File folder = getFolder(path);  
  467.         return new Date(folder.lastModified());  
  468.     }  
  469. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值