`
wusuoya
  • 浏览: 629799 次
  • 性别: Icon_minigender_2
  • 来自: 成都
社区版块
存档分类
最新评论

文件操作工具类

    博客分类:
  • Java
 
阅读更多

目录操作工具类 CopyDir.java

[java] view plaincopy
 
  1. package com.util;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** 
  6.  * 1,建立目的目录。 2,遍历源目录。 3,遍历过程中,创建文件或者文件夹。 原理:其实就是改变了源文件或者目录的目录头。 
  7.  * @datetime  Dsc  24 
  8.  */  
  9. public class CopyDir {  
  10.     private File sDir, dDir, newDir;  
  11.   
  12.     public CopyDir(String s, String d) {  
  13.         this(new File(s), new File(d));  
  14.     }  
  15.   
  16.     CopyDir(File sDir, File dDir)// c:\\Test d:\\abc  
  17.     {  
  18.         this.sDir = sDir;  
  19.         this.dDir = dDir;  
  20.     }  
  21.   
  22.     public void copyDir() throws IOException {  
  23.         // 是创建目的目录。也就是创建要拷贝的源文件夹。Test  
  24.         // 获取源文件夹名称。  
  25.         String name = sDir.getName();  
  26.         // 通过该名称在目的目录创建该文件夹,为了存放源文件夹中的文件或者文件夹。  
  27.         // 将目的目录和源文件夹名称,封装成File对象。  
  28.         newDir = dDir;  
  29.         // new File(dDir,name);  
  30.         // 调用该对象的mkdir方法。在目的目录创建该文件夹。d:\\abc\\Test  
  31.         newDir.mkdir();//  
  32.   
  33.         // 遍历源文件夹。  
  34.         listAll(sDir);  
  35.     }  
  36.   
  37.     /* 
  38.      * 将遍历目录封装成方法。 在遍历过程中,遇到文件创建文件。 遇到目录创建目录。 
  39.      */  
  40.     private void listAll(File dir) throws IOException {  
  41.         File[] files = dir.listFiles();  
  42.         for (int x = 0; x < files.length; x++) {  
  43.             if (files[x].isDirectory()) {  
  44.                 createDir(files[x]);// 调用创建目录的方法。  
  45.                 listAll(files[x]);// 在继续进行递归。进入子级目录。  
  46.             } else {  
  47.                 createFile(files[x]);// 调用创建文件的方法。  
  48.             }  
  49.         }  
  50.     }  
  51.   
  52.     /* 
  53.      * copy目录。通过源目录在目的目录创建新目录。 
  54.      */  
  55.     private void createDir(File dir) {  
  56.         File d = replaceFile(dir);  
  57.         d.mkdir();  
  58.     }  
  59.   
  60.     /* 
  61.      * copy文件。 
  62.      */  
  63.     private void createFile(File file) throws IOException {  
  64.         File newFile = replaceFile(file);  
  65.         // copy文件是一个数据数据传输的过程。需要通过流来完成。  
  66.         FileInputStream fis = new FileInputStream(file);  
  67.         FileOutputStream fos = new FileOutputStream(newFile);  
  68.         byte[] buf = new byte[1024 * 2];  
  69.         int num = 0;  
  70.         while ((num = fis.read(buf)) != -1) {  
  71.             fos.write(buf, 0, num);  
  72.         }  
  73.         fos.close();  
  74.         fis.close();  
  75.     }  
  76.   
  77.     /* 
  78.      * 替换路径。 
  79.      */  
  80.     private File replaceFile(File f) {  
  81.         // 原理是:将源目录的父目录(C:\\Tset),替换成目的父目录。(d:\\abc\\Test)  
  82.         String path = f.getAbsolutePath();// 获取源文件或者文件夹的决定路径。  
  83.         // 将源文件或者文件夹的绝对路径替换成目的路径。  
  84.         String newPath = path.replace(sDir.getAbsolutePath(), newDir  
  85.                 .getAbsolutePath());  
  86.         // 将新的目的路径封装成File对象  
  87.         File newFile = new File(newPath);  
  88.         return newFile;  
  89.     }  
  90. }  

文件/目录部分处理工具类 DealDir.java

[java] view plaincopy
 
  1. package com.util;  
  2.   
  3. import java.io.File;  
  4. import java.util.StringTokenizer;  
  5.   
  6. /** 
  7.  * 文件/目录 部分处理 
  8.  * @createTime Dec 25, 2010 7:06:58 AM 
  9.  * @version 1.0 
  10.  */  
  11. public class DealDir {  
  12.     /** 
  13.      * 获取文件的后缀名并转化成大写 
  14.      *  
  15.      * @param fileName 
  16.      *            文件名 
  17.      * @return 
  18.      */  
  19.     public String getFileSuffix(String fileName) throws Exception {  
  20.         return fileName.substring(fileName.lastIndexOf(".") + 1,  
  21.                 fileName.length()).toUpperCase();  
  22.     }  
  23.   
  24.     /** 
  25.      * 创建多级目录 
  26.      *  
  27.      * @param path 
  28.      *            目录的绝对路径 
  29.      */  
  30.     public void createMultilevelDir(String path) {  
  31.         try {  
  32.             StringTokenizer st = new StringTokenizer(path, "/");  
  33.             String path1 = st.nextToken() + "/";  
  34.             String path2 = path1;  
  35.             while (st.hasMoreTokens()) {  
  36.   
  37.                 path1 = st.nextToken() + "/";  
  38.                 path2 += path1;  
  39.                 File inbox = new File(path2);  
  40.                 if (!inbox.exists())  
  41.                     inbox.mkdir();  
  42.   
  43.             }  
  44.         } catch (Exception e) {  
  45.             System.out.println("目录创建失败" + e);  
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.     }  
  50.   
  51.     /** 
  52.      * 删除文件/目录(递归删除文件/目录) 
  53.      *  
  54.      * @param path 
  55.      *            文件或文件夹的绝对路径 
  56.      */  
  57.     public void deleteAll(String dirpath) {  
  58.         if (dirpath == null) {  
  59.             System.out.println("目录为空");  
  60.         } else {  
  61.             File path = new File(dirpath);  
  62.             try {  
  63.                 if (!path.exists())  
  64.                     return;// 目录不存在退出  
  65.                 if (path.isFile()) // 如果是文件删除  
  66.                 {  
  67.                     path.delete();  
  68.                     return;  
  69.                 }  
  70.                 File[] files = path.listFiles();// 如果目录中有文件递归删除文件  
  71.                 for (int i = 0; i < files.length; i++) {  
  72.                     deleteAll(files[i].getAbsolutePath());  
  73.                 }  
  74.                 path.delete();  
  75.   
  76.             } catch (Exception e) {  
  77.                 System.out.println("文件/目录 删除失败" + e);  
  78.                 e.printStackTrace();  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83.     /** 
  84.      * 文件/目录 重命名 
  85.      *  
  86.      * @param oldPath 
  87.      *            原有路径(绝对路径) 
  88.      * @param newPath 
  89.      *            更新路径 
  90.      * @author lyf 注:不能修改上层次的目录 
  91.      */  
  92.     public void renameDir(String oldPath, String newPath) {  
  93.         File oldFile = new File(oldPath);// 文件或目录  
  94.         File newFile = new File(newPath);// 文件或目录  
  95.         try {  
  96.             boolean success = oldFile.renameTo(newFile);// 重命名  
  97.             if (!success) {  
  98.                 System.out.println("重命名失败");  
  99.             } else {  
  100.                 System.out.println("重命名成功");  
  101.             }  
  102.         } catch (RuntimeException e) {  
  103.             e.printStackTrace();  
  104.         }  
  105.   
  106.     }  
  107.   
  108. }  

 

目录处理工具类 DealWithDir.java

[java] view plaincopy
 
  1. package com.util;  
  2.   
  3. import java.io.File;  
  4.   
  5. /** 
  6.  * 目录处理工具类 
  7.  * 
  8.  */  
  9. public class DealWithDir {  
  10.     /** 
  11.      * 新建目录 
  12.      */  
  13.     public static boolean newDir(String path) throws Exception {  
  14.         File file = new File(path);  
  15.         return file.mkdirs();//创建目录  
  16.     }  
  17.       
  18.     /** 
  19.      * 删除目录 
  20.      */  
  21.     public static boolean deleteDir(String path) throws Exception {  
  22.         File file = new File(path);  
  23.         if (!file.exists())  
  24.             return false;// 目录不存在退出  
  25.         if (file.isFile()) // 如果是文件删除  
  26.         {  
  27.             file.delete();  
  28.             return false;  
  29.         }  
  30.         File[] files = file.listFiles();// 如果目录中有文件递归删除文件  
  31.         for (int i = 0; i < files.length; i++) {  
  32.             deleteDir(files[i].getAbsolutePath());  
  33.         }  
  34.         file.delete();  
  35.           
  36.         return file.delete();//删除目录  
  37.     }  
  38.   
  39.     /** 
  40.      * 更新目录 
  41.      */  
  42.     public static boolean updateDir(String path, String newPath) throws Exception {  
  43.         File file = new File(path);  
  44.         File newFile = new File(newPath);  
  45.         return file.renameTo(newFile);  
  46.     }  
  47.       
  48.     public static void main(String d[]) throws Exception{  
  49.         //deleteDir("d:/ff/dddf");  
  50.         updateDir("D:\\TOOLS\\Tomcat 6.0\\webapps\\BCCCSM\\nationalExperiment/22222""D:\\TOOLS\\Tomcat 6.0\\webapps\\BCCCSM\\nationalExperiment/224222");  
  51.     }  
  52.       
  53.       
  54. }  

 

删除文件夹工具类 DeleteFolder.java

 

[java] view plaincopy
 
  1. package com.util;  
  2.   
  3. import java.io.File;  
  4.   
  5. /** 
  6.  * 删除文件夹 
  7.  * @createTime DSC 20, 2010 15:38 
  8.  * @version 2.0 
  9.  */  
  10. public class DeleteFolder {  
  11.     // 删除文件夹  
  12.     // param folderPath 文件夹完整绝对路径  
  13.     public static void delFolder(String folderPath) {  
  14.         try {  
  15.             delAllFile(folderPath); // 删除完里面所有内容  
  16.             String filePath = folderPath;  
  17.             filePath = filePath.toString();  
  18.             java.io.File myFilePath = new java.io.File(filePath);  
  19.             myFilePath.delete(); // 删除空文件夹  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25.     // 删除指定文件夹下所有文件  
  26.     // param path 文件夹完整绝对路径  
  27.     public static boolean delAllFile(String path) {  
  28.         boolean flag = false;  
  29.         File file = new File(path);  
  30.         if (!file.exists()) {  
  31.             return flag;  
  32.         }  
  33.         if (!file.isDirectory()) {  
  34.             return flag;  
  35.         }  
  36.         String[] tempList = file.list();  
  37.         File temp = null;  
  38.         for (int i = 0; i < tempList.length; i++) {  
  39.             if (path.endsWith(File.separator)) {  
  40.                 temp = new File(path + tempList[i]);  
  41.             } else {  
  42.                 temp = new File(path + File.separator + tempList[i]);  
  43.             }  
  44.             if (temp.isFile()) {  
  45.                 temp.delete();  
  46.             }  
  47.             if (temp.isDirectory()) {  
  48.                 delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件  
  49.                 delFolder(path + "/" + tempList[i]);// 再删除空文件夹  
  50.                 flag = true;  
  51.             }  
  52.         }  
  53.         return flag;  
  54.     }  
  55.   
  56. }  


 

文件上传工具类 UploadUtil.java

 

[java] view plaincopy
 
  1. package com.util;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10. import java.util.Calendar;  
  11.   
  12. /** 
  13.  * 文件上传工具类 
  14.  * 
  15.  */  
  16. public class UploadUtil {  
  17.     private static final int BUFFER_SIZE = 16 * 1024;  
  18.     //保存图片  
  19.     public static synchronized void copy(File src, File newFile) {  
  20.           
  21.         try {  
  22.             InputStream is = null;  
  23.             OutputStream os = null;  
  24.             try {  
  25.                 is = new BufferedInputStream(new FileInputStream(src),  
  26.                         BUFFER_SIZE);  
  27.                 os = new BufferedOutputStream(new FileOutputStream(newFile),  
  28.                         BUFFER_SIZE);  
  29.                 byte[] buffer = new byte[BUFFER_SIZE];  
  30.                 while (is.read(buffer) > 0) {  
  31.                     os.write(buffer);  
  32.                 }  
  33.             } finally {  
  34.                 if (null != is) {  
  35.                     is.close();  
  36.                 }  
  37.                 if (null != os) {  
  38.                     os.close();  
  39.                 }  
  40.             }  
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45.   
  46.     /** 
  47.      * 返回 年号+月号+天+时+分+秒+随机码 
  48.      * @return 
  49.      */  
  50.     @SuppressWarnings("static-access")  
  51.     public static synchronized String getTime() {  
  52.         Calendar calendar = Calendar.getInstance();  
  53.         String year = calendar.get(calendar.YEAR) + "";  
  54.         String month = (calendar.get(calendar.MONTH) + 1) + "";  
  55.         String day = calendar.get(calendar.DAY_OF_MONTH) + "";  
  56.         String hour = calendar.get(calendar.HOUR_OF_DAY) + "";  
  57.         String minute = calendar.get(calendar.MINUTE) + "";  
  58.         String second = calendar.get(calendar.SECOND) + "";  
  59.         String milliSecond = calendar.get(calendar.MILLISECOND) + "";  
  60.         int r = (int)(Math.random()*100000);  
  61.         String random = String.valueOf(r);  
  62.         return year + month + day + hour + minute + second + milliSecond + random+"a";  
  63.     }  
  64.   
  65. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics