文件处理

 
  1. import java.awt.Rectangle;
  2. import java.awt.Robot;
  3. import java.awt.image.BufferedImage;
  4. import java.io.BufferedInputStream;
  5. import java.io.BufferedOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Enumeration;
  13. import java.util.List;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipFile;
  16. import javax.imageio.ImageIO;
  17. import org.apache.log4j.Logger;
  18. /**
  19.  * 处理文件的基本操作。
  20.  *
  21.  * @author 
  22.  * @version 2005.12.16_02.
  23.  * @version 2006.2.22 add getAllFiles
  24.  * @version 2006.4.3 add FILE_SEPARATOR;
  25.  * @version 2006.4.22 add getSize();
  26.  */
  27. public class FileFiend {
  28.     public static final String FILE_SEPARATOR = System
  29.             .getProperty("file.separator");
  30.     private final static Logger logger = Logger.getLogger(FileFiend.class);
  31.     /**
  32.      * 删除指定的文件夹。
  33.      *
  34.      * @param directoryPath
  35.      * @return 是否成功 成功=true.
  36.      */
  37.     public static boolean deleteDirectory(String directoryPath) {
  38.         File file;
  39.         File[] files;
  40.         file = new File(directoryPath);
  41.         if (file != null) {
  42.             if (file.exists() && file.isDirectory()) {
  43.                 files = file.listFiles();
  44.                 if (files != null) {
  45.                     for (int i = 0; i < files.length; i++) {
  46.                         if (files[i].exists()) {
  47.                             if (files[i].isDirectory()) {
  48.                                 deleteDirectory(files[i].getPath());
  49.                             } else if (files[i].isFile()) {
  50.                                 boolean isHad = files[i].delete();
  51.                                 if (!isHad) {
  52.                                     logger
  53.                                             .info("delete file <<<<<<<faile>>>>>>>:"
  54.                                                     + files[i]);
  55.                                     return false;
  56.                                 }
  57.                             }
  58.                         }
  59.                     }
  60.                     file.delete();
  61.                 }
  62.             } else if (!file.exists() && file.isDirectory()) {
  63.                 logger.debug("directory do not exists!:" + directoryPath
  64.                         + " at _File.deleteDirectory()");
  65.                 return true;
  66.             }
  67.         } else {
  68.             logger.debug("file Object is null!:path=" + directoryPath);
  69.             return false;
  70.         }
  71.         return true;
  72.     }
  73.     /**
  74.      * 删除指定的文件
  75.      *
  76.      * @param filePath
  77.      * @return int:1=delete sucess;0=delete <<<<<<<faile>>>>>>>;2=no such
  78.      *         file;
  79.      */
  80.     public static int deleteFile(String filePath) {
  81.         File file = new File(filePath);
  82.         if (file != null) {
  83.             if (file.exists()) {
  84.                 if (file.delete()) {
  85.                     logger.debug("delete the file sucess: " + filePath);
  86.                     return 1;
  87.                 } else {
  88.                     logger.debug("delete the file <<<<<<<faile>>>>>>>: "
  89.                             + filePath);
  90.                     return 0;
  91.                 }
  92.             } else {
  93.                 logger.debug("this file do not existe :" + filePath);
  94.                 return 2;
  95.             }
  96.         } else {
  97.             logger.debug("file Object is null!:path=" + filePath);
  98.             return 2;
  99.         }
  100.     }
  101.     /**
  102.      * 从String中创建文件,如果此文件存在覆盖掉,如果不存在创建此文件。
  103.      *
  104.      * @param filePath
  105.      * @param fileData
  106.      * @return 写文件是否成功.
  107.      * @throws IOException
  108.      */
  109.     public static boolean writeFile(String filePath, String fileData,
  110.             String charsetName) {
  111.         if (filePath == null || fileData == null) {
  112.             logger.debug("the fileName or fileData is null: fileName="
  113.                     + filePath + " fileData=" + fileData);
  114.             return false;
  115.         } else if (filePath.equals("") || filePath.trim().equals("")) {
  116.             logger.debug("the fileName or fileData is   : fileName=" + filePath
  117.                     + " fileData=" + fileData);
  118.             return false;
  119.         }
  120.         FileOutputStream fileOutputStream = null;
  121.         try {
  122.             byte[] data = fileData.getBytes(charsetName);
  123.             File file = new File(filePath);
  124.             if (!file.exists()) {
  125.                 logger.debug("this file is not exist!:" + filePath);
  126.                 file.createNewFile();
  127.                 logger.debug("creat file!:" + filePath);
  128.             }
  129.             fileOutputStream = new FileOutputStream(filePath);
  130.             fileOutputStream.write(data);
  131.             fileOutputStream.close();
  132.             logger.debug("write file:" + filePath);
  133.             return true;
  134.         } catch (FileNotFoundException e) {
  135.             logger.debug(e.getMessage());
  136.         } catch (IOException e) {
  137.             logger.debug(e.getMessage());
  138.         } finally {
  139.             if (fileOutputStream != null) {
  140.                 try {
  141.                     fileOutputStream.close();
  142.                 } catch (IOException e) {
  143.                     logger.debug(e.toString());
  144.                     e.printStackTrace();
  145.                 }
  146.             }
  147.         }
  148.         return false;
  149.     }
  150.     /**
  151.      * 将byte[]的数据写入文件,如果此文件存在覆盖掉,如果不存在创建此文件。
  152.      *
  153.      * @param filePath
  154.      *            文件全路径.
  155.      * @param fileData
  156.      *            文件数据.
  157.      * @return 写文件是否成功.
  158.      */
  159.     public static boolean writeFile(String filePath, byte[] fileData) {
  160.         if (filePath == null || fileData == null) {
  161.             logger.debug("filePath or fileData is null");
  162.             return false;
  163.         } else if (filePath.trim().equals("")) {
  164.             logger.debug("filePath is /"/"!");
  165.             return false;
  166.         }
  167.         FileOutputStream write;
  168.         try {
  169.             write = new FileOutputStream(filePath);
  170.             write.write(fileData);
  171.             write.close();
  172.             logger.debug("write file:" + filePath + " success!");
  173.             return true;
  174.         } catch (FileNotFoundException e) {
  175.             logger.debug(e.getMessage());
  176.         } catch (IOException e) {
  177.             logger.debug(e.getMessage());
  178.         }
  179.         return false;
  180.     }
  181.     /**
  182.      * 从String中创建文件,如果此文件存在覆盖掉,如果不存在创建此文件,如果文件路径上的目录没有则创建此目录。
  183.      *
  184.      * @param filePath
  185.      * @param fileData
  186.      * @return 写文件是否成功.
  187.      * @throws IOException
  188.      */
  189.     public static boolean directWriteFile(String filePath, String fileData,
  190.             String charsetName) {
  191.         if (filePath == null || fileData == null) {
  192.             logger.debug("the fileName or fileData is null: fileName="
  193.                     + filePath + " fileData=" + fileData);
  194.             return false;
  195.         } else if (filePath.equals("") || filePath.trim().equals("")) {
  196.             logger.debug("the fileName or fileData is   : fileName=" + filePath
  197.                     + " fileData=" + fileData);
  198.             return false;
  199.         }
  200.         String fileDir = filePath.substring(0, filePath.lastIndexOf(System
  201.                 .getProperty("file.separator")));
  202.         boolean flag = makeDirectory(fileDir);
  203.         if (!flag) {
  204.             return false;
  205.         }
  206.         FileOutputStream fileOutputStream = null;
  207.         try {
  208.             byte[] data = fileData.getBytes(charsetName);
  209.             File file = new File(filePath);
  210.             if (!file.exists()) {
  211.                 logger.debug("this file is not exist!:" + filePath);
  212.                 file.createNewFile();
  213.                 logger.debug("creat file!:" + filePath);
  214.             }
  215.             fileOutputStream = new FileOutputStream(filePath);
  216.             fileOutputStream.write(data);
  217.             fileOutputStream.close();
  218.             logger.debug("write file:" + filePath);
  219.             return true;
  220.         } catch (FileNotFoundException e) {
  221.             logger.debug(e.getMessage());
  222.         } catch (IOException e) {
  223.             logger.debug(e.getMessage());
  224.         } finally {
  225.             if (fileOutputStream != null) {
  226.                 try {
  227.                     fileOutputStream.close();
  228.                 } catch (IOException e) {
  229.                     logger.debug(e.toString());
  230.                     e.printStackTrace();
  231.                 }
  232.             }
  233.         }
  234.         return false;
  235.     }
  236.     /**
  237.      * 将byte[]的数据写入文件,如果此文件存在则覆盖掉,如果不存在创建此文件,目录不存在也直接创建其目录。
  238.      *
  239.      * @param filePath
  240.      *            文件全路径.
  241.      * @param fileData
  242.      *            文件数据.
  243.      * @return 写文件是否成功.
  244.      */
  245.     public static boolean directWriteFile(String filePath, byte[] fileData) {
  246.         if (filePath == null || fileData == null) {
  247.             logger.debug("filePath or fileData is null");
  248.             return false;
  249.         } else if (filePath.trim().equals("")) {
  250.             logger.debug("filePath is /"/"!");
  251.             return false;
  252.         }
  253.         String fileDir = filePath.substring(0, filePath.lastIndexOf(System
  254.                 .getProperty("file.separator")));
  255.         boolean flag = makeDirectory(fileDir);
  256.         if (!flag) {
  257.             return false;
  258.         }
  259.         FileOutputStream write;
  260.         try {
  261.             write = new FileOutputStream(filePath);
  262.             write.write(fileData);
  263.             write.close();
  264.             logger.debug("write file:" + filePath + " success!");
  265.             return true;
  266.         } catch (FileNotFoundException e) {
  267.             logger.debug(e.getMessage());
  268.         } catch (IOException e) {
  269.             logger.debug(e.getMessage());
  270.         }
  271.         return false;
  272.     }
  273.     /**
  274.      * 将指定的文件解压缩到指定的文件夹,解压后的文件夹目录和给定的压缩文件名相同.
  275.      *
  276.      * @param zipFilePath
  277.      *            全路径
  278.      * @param unZipDirectory
  279.      *            全路径
  280.      * @return 解压缩文件是否成功.
  281.      * @throws IOException
  282.      */
  283.     public static boolean unZipFile(String zipFilePath, String unZipDirectory)
  284.             throws IOException {
  285.         ZipFile zipFile = new ZipFile(zipFilePath);
  286.         Enumeration entries = zipFile.entries();
  287.         if (zipFile == null) {
  288.             return false;
  289.         }
  290.         while (entries.hasMoreElements()) {
  291.             ZipEntry zipEntry = (ZipEntry) entries.nextElement();
  292.             File f = new File(unZipDirectory + FILE_SEPARATOR
  293.                     + zipEntry.getName());
  294.             if (zipEntry.isDirectory()) {
  295.                 if (!f.exists() && !f.mkdirs())
  296.                     throw new IOException("Couldn't create directory: " + f);
  297.             } else {
  298.                 BufferedInputStream is = null;
  299.                 BufferedOutputStream os = null;
  300.                 try {
  301.                     is = new BufferedInputStream(zipFile
  302.                             .getInputStream(zipEntry));
  303.                     File destDir = f.getParentFile();
  304.                     if (!destDir.exists() && !destDir.mkdirs()) {
  305.                         throw new IOException("Couldn't create dir " + destDir);
  306.                     }
  307.                     os = new BufferedOutputStream(new FileOutputStream(f));
  308.                     int b = -1;
  309.                     while ((b = is.read()) != -1) {
  310.                         os.write(b);
  311.                     }
  312.                 } finally {
  313.                     if (is != null)
  314.                         is.close();
  315.                     if (os != null)
  316.                         os.close();
  317.                 }
  318.             }
  319.         }
  320.         return true;
  321.     }
  322.     /**
  323.      * 将给定的文件cut到指定的全路径,如果该路径不存在自动创建该路径。
  324.      *
  325.      * @param filePath
  326.      *            全路径。
  327.      * @param toDirectory
  328.      *            全路径。
  329.      * @return 是否成功 成功==true.
  330.      */
  331.     public static boolean cutFile(String filePath, String toDirectory) {
  332.         if (filePath == null || toDirectory == null) {
  333.             logger
  334.                     .info("the filePath or toDirectory is null ! when cut this file: "
  335.                             + filePath + "---- to:" + toDirectory);
  336.             return false;
  337.         } else if (filePath.trim().equals("") || toDirectory.trim().equals("")) {
  338.             logger
  339.                     .info("the filePath or toDirectory is /"/"! when cut this file: "
  340.                             + filePath + "---- to:" + toDirectory);
  341.             return false;
  342.         }
  343.         if (copyFile(filePath, toDirectory)) {
  344.             int delte = deleteFile(filePath);
  345.             if (delte == 1) {
  346.                 return true;
  347.             } else {
  348.                 logger
  349.                         .info("copy the file sucess form "
  350.                                 + filePath
  351.                                 + "----to:"
  352.                                 + toDirectory
  353.                                 + " but delete this file <<<<<<<faile>>>>>>> when cut this file.");
  354.                 return false;
  355.             }
  356.         } else {
  357.             logger.debug("copy the file <<<<<<<faile>>>>>>> form " + filePath
  358.                     + "----to:" + toDirectory + " when cut this file.");
  359.             return false;
  360.         }
  361.     }
  362.     /**
  363.      * 将指定的文件夹cut到指定的路径下,如果该路径不存在,将窜关键该路径。
  364.      *
  365.      * @param directory
  366.      *            全路径
  367.      * @param toDirectory
  368.      *            全路径
  369.      * @return 是否成功 成功==true.
  370.      */
  371.     public static boolean cutDirectory(String directory, String toDirectory) {
  372.         if (copyDirectory(directory, toDirectory)) {
  373.             boolean isDelete = deleteDirectory(directory);
  374.             if (isDelete) {
  375.                 logger.debug("cut directory success: form"
  376.                         + " directory ---- to " + toDirectory);
  377.                 return true;
  378.             } else {
  379.                 logger
  380.                         .info("copy directory sucess but delete <<<<<<<faile>>>>>>> when cut the directory form"
  381.                                 + " directory ---- to " + toDirectory);
  382.                 return false;
  383.             }
  384.         } else {
  385.             return false;
  386.         }
  387.     }
  388.     /**
  389.      * 判断是文件系统中是否存在此文件。
  390.      *
  391.      * @param filePath
  392.      *            全路径
  393.      * @return 文件在系统中=true.
  394.      */
  395.     public static boolean isHave(String filePath) {
  396.         if (filePath == null) {
  397.             logger
  398.                     .info("not know file isHave .filePath is null! at _File.isHave");
  399.             return true;
  400.         } else if (filePath.trim().equals("")) {
  401.             logger
  402.                     .info("not know file isHave . filePath is /"/"! at _File.isHave");
  403.             return true;
  404.         }
  405.         File file = new File(filePath);
  406.         if (file.exists()) {
  407.             return true;
  408.         }
  409.         return false;
  410.     }
  411.     /**
  412.      * 将指定的文件copy到指定的路径下,如果该路径不存在将创建此路径。
  413.      *
  414.      * @param filePath
  415.      *            全路径
  416.      * @param toDirectoryPath
  417.      *            全路径
  418.      * @return 是否成功 成功==true.
  419.      */
  420.     public static boolean copyFile(String filePath, String toDirectoryPath) {
  421.         if (filePath == null || toDirectoryPath == null) {
  422.             logger
  423.                     .info("the filePath or toDirectory is null at _File.copyFile()");
  424.             return false;
  425.         } else if (filePath.trim().equals("")
  426.                 || toDirectoryPath.trim().equals("")) {
  427.             logger
  428.                     .info("the filePath or toDirectory is /"/" at _File.copuFile()");
  429.             return false;
  430.         }
  431.         File directory = new File(toDirectoryPath);
  432.         if (!directory.exists()) {
  433.             makeDirectory(toDirectoryPath);
  434.         }
  435.         File file = new File(filePath);
  436.         String toFilePath = toDirectoryPath + FILE_SEPARATOR + file.getName();
  437.         File toFile = new File(toFilePath);
  438.         try {
  439.             if (!file.isFile()) {
  440.                 logger.debug("can not get FileInputStream from this file:"
  441.                         + filePath);
  442.                 return false;
  443.             }
  444.             FileInputStream fis = new FileInputStream(file);
  445.             FileOutputStream fos = new FileOutputStream(toFile);
  446.             byte[] data = new byte[fis.available()];
  447.             int bytesRead;
  448.             while ((bytesRead = fis.read(data)) != -1) {
  449.                 fos.write(data, 0, bytesRead);
  450.             }
  451.             fos.flush();
  452.             fos.close();
  453.             fis.close();
  454.             logger
  455.                     .info("copy file file:" + file + "-----to:"
  456.                             + toDirectoryPath);
  457.             return true;
  458.         } catch (FileNotFoundException e) {
  459.             logger.debug(e.getMessage());
  460.         } catch (IOException e) {
  461.             logger.debug(e.getMessage());
  462.         }
  463.         return false;
  464.     }
  465.     /**
  466.      * 将指定的文件copy到指定的路径下,如果该路径不存在将创建此路径。
  467.      *
  468.      * @param sfilePath
  469.      *            全路径
  470.      * @param toDirectoryPath
  471.      *            全路径
  472.      * @return 是否成功 成功==true.
  473.      */
  474.     public static boolean copyFileByFilePath(String sfilePath, String dfilePath) {
  475.         if (sfilePath == null || dfilePath == null) {
  476.             logger.info("the filePath is null at _File.copyFile()");
  477.             return false;
  478.         } else if (sfilePath.trim().equals("") || dfilePath.trim().equals("")) {
  479.             logger.info("the filePath is /"/" at _File.copuFile()");
  480.             return false;
  481.         }
  482.         String toFileDir = dfilePath.substring(0, dfilePath.lastIndexOf(System
  483.                 .getProperty("file.separator")));
  484.         boolean flag = makeDirectory(toFileDir);
  485.         if (!flag) {
  486.             return false;
  487.         }
  488.         File file = new File(sfilePath);
  489.         File toFile = new File(dfilePath);
  490.         try {
  491.             if (!file.isFile()) {
  492.                 logger.debug("can not get FileInputStream from this file:"
  493.                         + sfilePath);
  494.                 return false;
  495.             }
  496.             FileInputStream fis = new FileInputStream(file);
  497.             FileOutputStream fos = new FileOutputStream(toFile);
  498.             byte[] data = new byte[fis.available()];
  499.             int bytesRead;
  500.             while ((bytesRead = fis.read(data)) != -1) {
  501.                 fos.write(data, 0, bytesRead);
  502.             }
  503.             fos.flush();
  504.             fos.close();
  505.             fis.close();
  506.             logger.info("copy file file:" + file + "-----to:" + dfilePath);
  507.             return true;
  508.         } catch (FileNotFoundException e) {
  509.             logger.debug(e.getMessage());
  510.         } catch (IOException e) {
  511.             logger.debug(e.getMessage());
  512.         }
  513.         return false;
  514.     }
  515.     /**
  516.      * 将指定的文件夹copy到指定的路径下,如果指定的制定的路径不存在,创建此路径;指定的路径存在将覆盖.
  517.      *
  518.      * @param directoryPath
  519.      *            全路径
  520.      * @param toDirectoryPath
  521.      *            全路径
  522.      * @return 是否成功 成功==true.
  523.      */
  524.     public static boolean copyDirectory(String directoryPath,
  525.             String toDirectoryPath) {
  526.         File file;
  527.         File[] files;
  528.         file = new File(directoryPath);
  529.         if (file.exists() && file.isDirectory()) {
  530.             files = file.listFiles();
  531.             if (files != null) {
  532.                 for (int i = 0; i < files.length; i++) {
  533.                     if (files[i].exists()) {
  534.                         if (files[i].isDirectory()) {
  535.                             copyDirectory(files[i].getPath(), toDirectoryPath
  536.                                     + FILE_SEPARATOR + files[i].getName());
  537.                         } else if (files[i].isFile()) {
  538.                             copyFile(files[i].getPath(), toDirectoryPath);
  539.                         }
  540.                     }
  541.                 }
  542.             }
  543.         } else {
  544.             logger.debug("file not exists or is not directory!");
  545.             return false;
  546.         }
  547.         logger.debug("copy directory form:" + directoryPath + "to :"
  548.                 + toDirectoryPath + ".");
  549.         return true;
  550.     }
  551.     /**
  552.      * 从给定的字符串中创建文件系统路径。
  553.      *
  554.      * @param directory
  555.      *            给定的路径表示字符串。
  556.      * @return 是否成功 成功==true.
  557.      */
  558.     public static boolean makeDirectory(String directory) {
  559.         File file = new File(directory);
  560.         if (!file.exists()) {
  561.             if (file.mkdirs()) {
  562.                 logger.debug("make dirctory success!:" + directory);
  563.                 return true;
  564.             } else {
  565.                 logger.debug("make dirctory <<<<<<<faile>>>>>>>!:" + directory);
  566.                 return false;
  567.             }
  568.         } else {
  569.             logger.debug("this directory is existed!:" + directory);
  570.             return true;
  571.         }
  572.     }
  573.     /**
  574.      * 从文件中读出数据放到String中.
  575.      *
  576.      * @param filePath
  577.      *            文件路径.
  578.      * @param encoding
  579.      *            编码格式.
  580.      * @return 文件的String.
  581.      */
  582.     public static String readFile(String filePath, String encoding) {
  583.         String contents = null;
  584.         FileInputStream fissrc;
  585.         try {
  586.             fissrc = new FileInputStream(filePath);
  587.             int len = fissrc.available();
  588.             byte[] data = new byte[len];
  589.             int actual = 0;
  590.             int bytesread = 0;
  591.             while ((bytesread != len) && (actual != -1)) {
  592.                 actual = fissrc.read(data, bytesread, len - bytesread);
  593.                 bytesread += actual;
  594.             }
  595.             contents = new String(data, "GBK");
  596.         } catch (FileNotFoundException e) {
  597.             e.printStackTrace();
  598.         } catch (IOException e) {
  599.             e.printStackTrace();
  600.         }
  601.         return contents;
  602.     }
  603.     /**
  604.      * 获得所有的文件名.
  605.      *
  606.      * @param directoryPath
  607.      * @return
  608.      */
  609.     public static List getAllFiles(String directoryPath) {
  610.         List allFiles = new ArrayList();
  611.         if (!isHave(directoryPath)) {
  612.             return allFiles;
  613.         } else {
  614.             File file = new File(directoryPath);
  615.             if (file.isDirectory()) {
  616.                 File[] files = file.listFiles();
  617.                 for (int i = 0; i < files.length; i++) {
  618.                     allFiles.add(files[i].getName());
  619.                 }
  620.                 return allFiles;
  621.             } else {
  622.                 return allFiles;
  623.             }
  624.         }
  625.     }
  626.     /**
  627.      * 不区别文件和文件夹地获取其大小.
  628.      *
  629.      * @param path
  630.      *            文件或者文件夹的大小.
  631.      * @return long 类型的大小 bytes
  632.      * @throws FileNotFoundException
  633.      * @since 2006.4.22
  634.      */
  635.     public static long getSize(String path) throws FileNotFoundException {
  636.         File file = new File(path);
  637.         if (file.isFile()) {
  638.             return getFileSize(path);
  639.         } else {
  640.             return getDirectorySize(path, 0);
  641.         }
  642.     }
  643.     /**
  644.      * 返回一个文件的大小.
  645.      *
  646.      * @param filePath
  647.      *            文件的全路径.
  648.      * @return long bytes value 文件大小.
  649.      * @throws FileNotFoundException
  650.      * @since 2006.4.22
  651.      */
  652.     public static long getFileSize(String filePath)
  653.             throws FileNotFoundException {
  654.         File file = new File(filePath);
  655.         if (!file.exists()) {
  656.             throw new FileNotFoundException("this file is not exited: "
  657.                     + filePath);
  658.         }
  659.         return file.length();
  660.     }
  661.     /**
  662.      * 返回一个文件夹的大小.
  663.      *
  664.      * @param directoryPath
  665.      *            文件夹的全路径.
  666.      * @return 文件夹大小.
  667.      * @throws FileNotFoundException
  668.      * @since 2006.4.22
  669.      */
  670.     public static long getDirectorySize(String directoryPath)
  671.             throws FileNotFoundException {
  672.         return getDirectorySize(directoryPath, 0);
  673.     }
  674.     /**
  675.      * 递归的获取文件夹的每一个文件的大小,并计算总的大小.
  676.      *
  677.      * @param directoryPath
  678.      *            文件夹大小.
  679.      * @param size
  680.      *            递归参数
  681.      * @return long bytes value;
  682.      * @throws FileNotFoundException
  683.      * @since 2006.4.22
  684.      */
  685.     private static long getDirectorySize(String directoryPath, long size)
  686.             throws FileNotFoundException {
  687.         File file = new File(directoryPath);
  688.         File[] files = file.listFiles();
  689.         for (int i = 0; i < files.length; i++) {
  690.             String filePath = files[i].getAbsolutePath();
  691.             if (files[i].isDirectory()) {
  692.                 size = getDirectorySize(filePath, size);
  693.             } else {
  694.                 size += getFileSize(filePath);
  695.             }
  696.         }
  697.         return size;
  698.     }
  699.     /**
  700.      * @author
  701.      * @param filePath
  702.      * @return long bytes value;
  703.      * @since 1.0.0 从文件中读出数据放到byte中.
  704.      * @createTime 2006-11-06 13:50:20
  705.      */
  706.     public static byte[] readFileByByte(String filePath) {
  707.         byte[] data = null;
  708.         FileInputStream fissrc;
  709.         try {
  710.             fissrc = new FileInputStream(filePath);
  711.             int len = fissrc.available();
  712.             data = new byte[len];
  713.             int actual = 0;
  714.             int bytesread = 0;
  715.             while ((bytesread != len) && (actual != -1)) {
  716.                 actual = fissrc.read(data, bytesread, len - bytesread);
  717.                 bytesread += actual;
  718.             }
  719.             fissrc.close();
  720.         } catch (FileNotFoundException e) {
  721.             e.printStackTrace();
  722.         } catch (IOException e) {
  723.             e.printStackTrace();
  724.         }
  725.         return data;
  726.     }
  727.     /**
  728.      * 删除非空目录
  729.      *
  730.      * @param dir
  731.      * @throws IOException
  732.      * @author
  733.      * @create 2008-8-30 下午12:46:18
  734.      * @since
  735.      */
  736.     public static void deleteDirectory(File dir) throws IOException {
  737.         if ((dir == null) || !dir.isDirectory()) {
  738.             throw new IllegalArgumentException("Argument " + dir
  739.                     + " is not a directory. ");
  740.         }
  741.         File[] entries = dir.listFiles();
  742.         int sz = entries.length;
  743.         for (int i = 0; i < sz; i++) {
  744.             if (entries[i].isDirectory()) {
  745.                 deleteDirectory(entries[i]);
  746.             } else {
  747.                 entries[i].delete();
  748.             }
  749.         }
  750.         dir.delete();
  751.     }
  752.     /**
  753.      * 捕捉屏幕
  754.      *
  755.      * @param fileName
  756.      * @author
  757.      * @param top
  758.      * @param left
  759.      * @create 2008-8-30 下午08:03:10
  760.      * @since
  761.      */
  762.     public static void snapShotsf(String fileName, int left, int top) {
  763.         try {
  764.             BufferedImage screenshot = (new Robot())
  765.                     .createScreenCapture(new Rectangle(left, top, 640526));
  766.             String name = fileName + ".jpg";
  767.             File f = new File(name);
  768.             ImageIO.write(screenshot, "jpg", f);
  769.         } catch (Exception ex) {
  770.             System.out.println(ex);
  771.         }
  772.     }
  773. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值