java.io.file 各种文件操作

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