文件操作FileUtil.java

 
  1. package com.work.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  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.io.InputStreamReader;
  12. import java.io.OutputStreamWriter;
  13. import java.io.RandomAccessFile;
  14. import java.io.StringReader;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipOutputStream;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. /**
  22.  * 日 期: 2008-2-14 12:05:18<br />
  23.  * project: zxj <br />
  24.  * 作 者:wangmingjie<br />
  25.  */
  26. public class FileUtil {
  27.     private static Log log = LogFactory.getLog(FileUtil.class);
  28.     /**
  29.      * 创建单个文件夹。
  30.      * 
  31.      * @param dir
  32.      * @param ignoreIfExitst
  33.      *            true 表示如果文件夹存在就不再创建了。false是重新创建。
  34.      * @throws IOException
  35.      */
  36.     public static void createDir(String dir, boolean ignoreIfExitst)
  37.             throws IOException {
  38.         File file = new File(dir);
  39.         if (ignoreIfExitst && file.exists()) {
  40.             return;
  41.         }
  42.         if (file.mkdir() == false) {
  43.             throw new IOException("Cannot create the directory = " + dir);
  44.         }
  45.     }
  46.     /**
  47.      * 创建多个文件夹
  48.      * 
  49.      * @param dir
  50.      * @param ignoreIfExitst
  51.      * @throws IOException
  52.      */
  53.     public static void createDirs(String dir, boolean ignoreIfExitst)
  54.             throws IOException {
  55.         File file = new File(dir);
  56.         if (ignoreIfExitst && file.exists()) {
  57.             return;
  58.         }
  59.         if (file.mkdirs() == false) {
  60.             throw new IOException("Cannot create directories = " + dir);
  61.         }
  62.     }
  63.     /**
  64.      * 删除一个文件。
  65.      * 
  66.      * @param filename
  67.      * @throws IOException
  68.      */
  69.     public static void deleteFile(String filename) throws IOException {
  70.         File file = new File(filename);
  71.         log.trace("Delete file = " + filename);
  72.         if (file.isDirectory()) {
  73.             throw new IOException(
  74.                     "IOException -> BadInputException: not a file.");
  75.         }
  76.         if (file.exists() == false) {
  77.             throw new IOException(
  78.                     "IOException -> BadInputException: file is not exist.");
  79.         }
  80.         if (file.delete() == false) {
  81.             throw new IOException("Cannot delete file. filename = " + filename);
  82.         }
  83.     }
  84.     /**
  85.      * 删除文件夹及其下面的子文件夹
  86.      * 
  87.      * @param dir
  88.      * @throws IOException
  89.      */
  90.     public static void deleteDir(File dir) throws IOException {
  91.         if (dir.isFile())
  92.             throw new IOException(
  93.                     "IOException -> BadInputException: not a directory.");
  94.         File[] files = dir.listFiles();
  95.         if (files != null) {
  96.             for (int i = 0; i < files.length; i++) {
  97.                 File file = files[i];
  98.                 if (file.isFile()) {
  99.                     file.delete();
  100.                 } else {
  101.                     deleteDir(file);
  102.                 }
  103.             }
  104.         }// if
  105.         dir.delete();
  106.     }
  107.     public static String getPathSeparator() {
  108.         return java.io.File.pathSeparator;
  109.     }
  110.     public static String getFileSeparator() {
  111.         return java.io.File.separator;
  112.     }
  113.     /**
  114.      * 列出指定文件目录下面的文件信息。
  115.      * 
  116.      * @param dir
  117.      * @return
  118.      * @throws IOException
  119.      */
  120.     public static List<FileInfo> getFiles(File dir) throws IOException {
  121.         if (dir.isFile())
  122.             throw new IOException("BadInputException: not a directory.");
  123.         if (!dir.exists()) {
  124.             throw new IOException(" don't exist ");
  125.         }
  126.         File[] files = dir.listFiles();
  127.         int LEN = 0;
  128.         if (files != null) {
  129.             LEN = files.length;
  130.         }
  131.         List<FileInfo> l = new ArrayList<FileInfo>();
  132.         long tempFLen = 0//文件长度
  133.         for (int i = 0; i < LEN; i++) {
  134.             FileInfo temp = new FileInfo();
  135.             temp.setFileName(files[i].getName());
  136.             temp.setIsDir(files[i].isDirectory());
  137.             //是文件,且包含.
  138.             if (files[i].isFile()) {
  139.                 if(files[i].getName().lastIndexOf(".")!=-1)
  140.                 temp.setFileType(files[i].getName().substring(
  141.                         files[i].getName().lastIndexOf(".")));
  142.             }else{
  143.                 temp.setFileType("文件夹");
  144.             }
  145.             tempFLen = files[i].length();
  146.             temp.setFileLen(tempFLen);
  147.             if(tempFLen/1024/1024/1024 >0){
  148.                 temp.setFileLength(files[i].length() / 1024/1024/1024 + "G");
  149.             }else if(tempFLen/1024/1024 >0){
  150.                 temp.setFileLength(files[i].length() / 1024/1024 + "M");
  151.             }else if(tempFLen/1024 >0){
  152.                 temp.setFileLength(files[i].length() / 1024 + "K");
  153.             }else{
  154.                 temp.setFileLength(tempFLen+"byte");
  155.             }
  156.             
  157.             temp.setFilePath(files[i].getAbsolutePath().replaceAll("[]""/"));
  158.             temp.setLastModifiedTime(com.work.util.DateUtil
  159.                     .getDateTime(files[i].lastModified()));
  160.             temp.setIsHidden(files[i].isHidden());
  161.             temp.setAuthor(null);
  162.             temp.setVersion(null);
  163.             temp.setFileClass(null);
  164.             temp.setRemark(null);
  165.             l.add(temp);
  166.         }
  167.         return l;
  168.     }
  169.     /**
  170.      * 获取到目录下面文件的大小。包含了子目录。
  171.      * 
  172.      * @param dir
  173.      * @return
  174.      * @throws IOException
  175.      */
  176.     public static long getDirLength(File dir) throws IOException {
  177.         if (dir.isFile())
  178.             throw new IOException("BadInputException: not a directory.");
  179.         long size = 0;
  180.         File[] files = dir.listFiles();
  181.         if (files != null) {
  182.             for (int i = 0; i < files.length; i++) {
  183.                 File file = files[i];
  184.                 // file.getName();
  185.                 // System.out.println(file.getName());
  186.                 long length = 0;
  187.                 if (file.isFile()) {
  188.                     length = file.length();
  189.                 } else {
  190.                     length = getDirLength(file);
  191.                 }
  192.                 size += length;
  193.             }// for
  194.         }// if
  195.         return size;
  196.     }
  197.     /**
  198.      * 将文件清空。
  199.      * 
  200.      * @param srcFilename
  201.      * @throws IOException
  202.      */
  203.     public static void emptyFile(String srcFilename) throws IOException {
  204.         File srcFile = new File(srcFilename);
  205.         if (!srcFile.exists()) {
  206.             throw new FileNotFoundException("Cannot find the file: "
  207.                     + srcFile.getAbsolutePath());
  208.         }
  209.         if (!srcFile.canWrite()) {
  210.             throw new IOException("Cannot write the file: "
  211.                     + srcFile.getAbsolutePath());
  212.         }
  213.         FileOutputStream outputStream = new FileOutputStream(srcFilename);
  214.         outputStream.close();
  215.     }
  216.     /**
  217.      * Write content to a fileName with the destEncoding 写文件。如果此文件不存在就创建一个。
  218.      * 
  219.      * @param content
  220.      *            String
  221.      * @param fileName
  222.      *            String
  223.      * @param destEncoding
  224.      *            String
  225.      * @throws FileNotFoundException
  226.      * @throws IOException
  227.      */
  228.     public static void writeFile(String content, String fileName,
  229.             String destEncoding) throws FileNotFoundException, IOException {
  230.         File file = null;
  231.         try {
  232.             file = new File(fileName);
  233.             if (!file.exists()) {
  234.                 if (file.createNewFile() == false) {
  235.                     throw new IOException("create file '" + fileName
  236.                             + "' failure.");
  237.                 }
  238.             }
  239.             if (file.isFile() == false) {
  240.                 throw new IOException("'" + fileName + "' is not a file.");
  241.             }
  242.             if (file.canWrite() == false) {
  243.                 throw new IOException("'" + fileName + "' is a read-only file.");
  244.             }
  245.         } finally {
  246.             // we dont have to close File here
  247.         }
  248.         BufferedWriter out = null;
  249.         try {
  250.             FileOutputStream fos = new FileOutputStream(fileName);
  251.             out = new BufferedWriter(new OutputStreamWriter(fos, destEncoding));
  252.             out.write(content);
  253.             out.flush();
  254.         } catch (FileNotFoundException fe) {
  255.             log.error("Error", fe);
  256.             throw fe;
  257.         } catch (IOException e) {
  258.             log.error("Error", e);
  259.             throw e;
  260.         } finally {
  261.             try {
  262.                 if (out != null)
  263.                     out.close();
  264.             } catch (IOException ex) {
  265.             }
  266.         }
  267.     }
  268.     /**
  269.      * 读取文件的内容,并将文件内容以字符串的形式返回。
  270.      * 
  271.      * @param fileName
  272.      * @param srcEncoding
  273.      * @return
  274.      * @throws FileNotFoundException
  275.      * @throws IOException
  276.      */
  277.     public static String readFile(String fileName, String srcEncoding)
  278.             throws FileNotFoundException, IOException {
  279.         File file = null;
  280.         try {
  281.             file = new File(fileName);
  282.             if (file.isFile() == false) {
  283.                 throw new IOException("'" + fileName + "' is not a file.");
  284.             }
  285.         } finally {
  286.             // we dont have to close File here
  287.         }
  288.         BufferedReader reader = null;
  289.         try {
  290.             StringBuffer result = new StringBuffer(1024);
  291.             FileInputStream fis = new FileInputStream(fileName);
  292.             reader = new BufferedReader(new InputStreamReader(fis, srcEncoding));
  293.             char[] block = new char[512];
  294.             while (true) {
  295.                 int readLength = reader.read(block);
  296.                 if (readLength == -1)
  297.                     break;// end of file
  298.                 result.append(block, 0, readLength);
  299.             }
  300.             return result.toString();
  301.         } catch (FileNotFoundException fe) {
  302.             log.error("Error", fe);
  303.             throw fe;
  304.         } catch (IOException e) {
  305.             log.error("Error", e);
  306.             throw e;
  307.         } finally {
  308.             try {
  309.                 if (reader != null)
  310.                     reader.close();
  311.             } catch (IOException ex) {
  312.             }
  313.         }
  314.     }
  315.     /*
  316.      * 1 ABC 2 abC Gia su doc tu dong 1 lay ca thay 5 dong => 1 --> 5 3 ABC
  317.      */
  318.     public static String[] getLastLines(File file, int linesToReturn)
  319.             throws IOException, FileNotFoundException {
  320.         final int AVERAGE_CHARS_PER_LINE = 250;
  321.         final int BYTES_PER_CHAR = 2;
  322.         RandomAccessFile randomAccessFile = null;
  323.         StringBuffer buffer = new StringBuffer(linesToReturn
  324.                 * AVERAGE_CHARS_PER_LINE);
  325.         int lineTotal = 0;
  326.         try {
  327.             randomAccessFile = new RandomAccessFile(file, "r");
  328.             long byteTotal = randomAccessFile.length();
  329.             long byteEstimateToRead = linesToReturn * AVERAGE_CHARS_PER_LINE
  330.                     * BYTES_PER_CHAR;
  331.             long offset = byteTotal - byteEstimateToRead;
  332.             if (offset < 0) {
  333.                 offset = 0;
  334.             }
  335.             randomAccessFile.seek(offset);
  336.             // log.debug("SKIP IS ::" + offset);
  337.             String line = null;
  338.             String lineUTF8 = null;
  339.             while ((line = randomAccessFile.readLine()) != null) {
  340.                 lineUTF8 = new String(line.getBytes("ISO8859_1"), "UTF-8");
  341.                 lineTotal++;
  342.                 buffer.append(lineUTF8).append("/n");
  343.             }
  344.         } finally {
  345.             if (randomAccessFile != null) {
  346.                 try {
  347.                     randomAccessFile.close();
  348.                 } catch (IOException ex) {
  349.                 }
  350.             }
  351.         }
  352.         String[] resultLines = new String[linesToReturn];
  353.         BufferedReader in = null;
  354.         try {
  355.             in = new BufferedReader(new StringReader(buffer.toString()));
  356.             int start = lineTotal /* + 2 */- linesToReturn; // Ex : 55 - 10 = 45
  357.             // ~ offset
  358.             if (start < 0)
  359.                 start = 0// not start line
  360.             for (int i = 0; i < start; i++) {
  361.                 in.readLine(); // loop until the offset. Ex: loop 0, 1 ~~ 2
  362.                 // lines
  363.             }
  364.             int i = 0;
  365.             String line = null;
  366.             while ((line = in.readLine()) != null) {
  367.                 resultLines[i] = line;
  368.                 i++;
  369.             }
  370.         } catch (IOException ie) {
  371.             log.error("Error" + ie);
  372.             throw ie;
  373.         } finally {
  374.             if (in != null) {
  375.                 try {
  376.                     in.close();
  377.                 } catch (IOException ex) {
  378.                 }
  379.             }
  380.         }
  381.         return resultLines;
  382.     }
  383.     /**
  384.      * 单个文件拷贝。
  385.      * 
  386.      * @param srcFilename
  387.      * @param destFilename
  388.      * @param overwrite
  389.      * @throws IOException
  390.      */
  391.     public static void copyFile(String srcFilename, String destFilename,
  392.             boolean overwrite) throws IOException {
  393.         File srcFile = new File(srcFilename);
  394.         // 首先判断源文件是否存在
  395.         if (!srcFile.exists()) {
  396.             throw new FileNotFoundException("Cannot find the source file: "
  397.                     + srcFile.getAbsolutePath());
  398.         }
  399.         // 判断源文件是否可读
  400.         if (!srcFile.canRead()) {
  401.             throw new IOException("Cannot read the source file: "
  402.                     + srcFile.getAbsolutePath());
  403.         }
  404.         File destFile = new File(destFilename);
  405.         if (overwrite == false) {
  406.             // 目标文件存在就不覆盖
  407.             if (destFile.exists())
  408.                 return;
  409.         } else {
  410.             // 如果要覆盖已经存在的目标文件,首先判断是否目标文件可写。
  411.             if (destFile.exists()) {
  412.                 if (!destFile.canWrite()) {
  413.                     throw new IOException("Cannot write the destination file: "
  414.                             + destFile.getAbsolutePath());
  415.                 }
  416.             } else {
  417.                 // 不存在就创建一个新的空文件。
  418.                 if (!destFile.createNewFile()) {
  419.                     throw new IOException("Cannot write the destination file: "
  420.                             + destFile.getAbsolutePath());
  421.                 }
  422.             }
  423.         }
  424.         BufferedInputStream inputStream = null;
  425.         BufferedOutputStream outputStream = null;
  426.         byte[] block = new byte[1024];
  427.         try {
  428.             inputStream = new BufferedInputStream(new FileInputStream(srcFile));
  429.             outputStream = new BufferedOutputStream(new FileOutputStream(
  430.                     destFile));
  431.             while (true) {
  432.                 int readLength = inputStream.read(block);
  433.                 if (readLength == -1)
  434.                     break;// end of file
  435.                 outputStream.write(block, 0, readLength);
  436.             }
  437.         } finally {
  438.             if (inputStream != null) {
  439.                 try {
  440.                     inputStream.close();
  441.                 } catch (IOException ex) {
  442.                     // just ignore
  443.                 }
  444.             }
  445.             if (outputStream != null) {
  446.                 try {
  447.                     outputStream.close();
  448.                 } catch (IOException ex) {
  449.                     // just ignore
  450.                 }
  451.             }
  452.         }
  453.     }
  454.     /**
  455.      * 单个文件拷贝。
  456.      * 
  457.      * @param srcFile
  458.      * @param destFile
  459.      * @param overwrite
  460.      *            是否覆盖目的文件
  461.      * @throws IOException
  462.      */
  463.     public static void copyFile(File srcFile, File destFile, boolean overwrite)
  464.             throws IOException {
  465.         // 首先判断源文件是否存在
  466.         if (!srcFile.exists()) {
  467.             throw new FileNotFoundException("Cannot find the source file: "
  468.                     + srcFile.getAbsolutePath());
  469.         }
  470.         // 判断源文件是否可读
  471.         if (!srcFile.canRead()) {
  472.             throw new IOException("Cannot read the source file: "
  473.                     + srcFile.getAbsolutePath());
  474.         }
  475.         if (overwrite == false) {
  476.             // 目标文件存在就不覆盖
  477.             if (destFile.exists())
  478.                 return;
  479.         } else {
  480.             // 如果要覆盖已经存在的目标文件,首先判断是否目标文件可写。
  481.             if (destFile.exists()) {
  482.                 if (!destFile.canWrite()) {
  483.                     throw new IOException("Cannot write the destination file: "
  484.                             + destFile.getAbsolutePath());
  485.                 }
  486.             } else {
  487.                 // 不存在就创建一个新的空文件。
  488.                 if (!destFile.createNewFile()) {
  489.                     throw new IOException("Cannot write the destination file: "
  490.                             + destFile.getAbsolutePath());
  491.                 }
  492.             }
  493.         }
  494.         BufferedInputStream inputStream = null;
  495.         BufferedOutputStream outputStream = null;
  496.         byte[] block = new byte[1024];
  497.         try {
  498.             inputStream = new BufferedInputStream(new FileInputStream(srcFile));
  499.             outputStream = new BufferedOutputStream(new FileOutputStream(
  500.                     destFile));
  501.             while (true) {
  502.                 int readLength = inputStream.read(block);
  503.                 if (readLength == -1)
  504.                     break;// end of file
  505.                 outputStream.write(block, 0, readLength);
  506.             }
  507.         } finally {
  508.             if (inputStream != null) {
  509.                 try {
  510.                     inputStream.close();
  511.                 } catch (IOException ex) {
  512.                     // just ignore
  513.                 }
  514.             }
  515.             if (outputStream != null) {
  516.                 try {
  517.                     outputStream.close();
  518.                 } catch (IOException ex) {
  519.                     // just ignore
  520.                 }
  521.             }
  522.         }
  523.     }
  524.     /**
  525.      * 拷贝文件,从源文件夹拷贝文件到目的文件夹。 <br>
  526.      * 参数源文件夹和目的文件夹,最后都不要带文件路径符号,例如:c:/aa正确,c:/aa/错误。
  527.      * 
  528.      * @param srcDirName
  529.      *            源文件夹名称 ,例如:c:/test/aa 或者c://test//aa
  530.      * @param destDirName
  531.      *            目的文件夹名称,例如:c:/test/aa 或者c://test//aa
  532.      * @param overwrite
  533.      *            是否覆盖目的文件夹下面的文件。
  534.      * @throws IOException
  535.      */
  536.     public static void copyFiles(String srcDirName, String destDirName,
  537.             boolean overwrite) throws IOException {
  538.         File srcDir = new File(srcDirName);// 声明源文件夹
  539.         // 首先判断源文件夹是否存在
  540.         if (!srcDir.exists()) {
  541.             throw new FileNotFoundException(
  542.                     "Cannot find the source directory: "
  543.                             + srcDir.getAbsolutePath());
  544.         }
  545.         File destDir = new File(destDirName);
  546.         if (overwrite == false) {
  547.             if (destDir.exists()) {
  548.                 // do nothing
  549.             } else {
  550.                 if (destDir.mkdirs() == false) {
  551.                     throw new IOException(
  552.                             "Cannot create the destination directories = "
  553.                                     + destDir);
  554.                 }
  555.             }
  556.         } else {
  557.             // 覆盖存在的目的文件夹
  558.             if (destDir.exists()) {
  559.                 // do nothing
  560.             } else {
  561.                 // create a new directory
  562.                 if (destDir.mkdirs() == false) {
  563.                     throw new IOException(
  564.                             "Cannot create the destination directories = "
  565.                                     + destDir);
  566.                 }
  567.             }
  568.         }
  569.         // 循环查找源文件夹目录下面的文件(屏蔽子文件夹),然后将其拷贝到指定的目的文件夹下面。
  570.         File[] srcFiles = srcDir.listFiles();
  571.         if (srcFiles == null || srcFiles.length < 1) {
  572.             // throw new IOException ("Cannot find any file from source
  573.             // directory!!!");
  574.             return;// do nothing
  575.         }
  576.         // 开始复制文件
  577.         int SRCLEN = srcFiles.length;
  578.         for (int i = 0; i < SRCLEN; i++) {
  579.             // File tempSrcFile = srcFiles[i];
  580.             File destFile = new File(destDirName + File.separator
  581.                     + srcFiles[i].getName());
  582.             // 注意构造文件对象时候,文件名字符串中不能包含文件路径分隔符";".
  583.             // log.debug(destFile);
  584.             if (srcFiles[i].isFile()) {
  585.                 copyFile(srcFiles[i], destFile, overwrite);
  586.             } else {
  587.                 // 在这里进行递归调用,就可以实现子文件夹的拷贝
  588.                 copyFiles(srcFiles[i].getAbsolutePath(), destDirName
  589.                         + File.separator + srcFiles[i].getName(), overwrite);
  590.             }
  591.         }
  592.     }
  593.     /**
  594.      * 压缩文件。注意:中文文件名称和中文的评论会乱码。
  595.      * @param srcFilename
  596.      * @param destFilename
  597.      * @param overwrite
  598.      * @throws IOException
  599.      */
  600.     public static void zipFile(String srcFilename, String destFilename,
  601.             boolean overwrite) throws IOException {
  602.         
  603.         File srcFile = new File(srcFilename);
  604.         // 首先判断源文件是否存在
  605.         if (!srcFile.exists()) {
  606.             throw new FileNotFoundException("Cannot find the source file: "
  607.                     + srcFile.getAbsolutePath());
  608.         }
  609.         // 判断源文件是否可读
  610.         if (!srcFile.canRead()) {
  611.             throw new IOException("Cannot read the source file: "
  612.                     + srcFile.getAbsolutePath());
  613.         }
  614.         if(destFilename==null || destFilename.trim().equals("")){
  615.             destFilename = srcFilename+".zip";
  616.         }else{
  617.             destFilename += ".zip";
  618.         }
  619.         File destFile = new File(destFilename);
  620.         if (overwrite == false) {
  621.             // 目标文件存在就不覆盖
  622.             if (destFile.exists())
  623.                 return;
  624.         } else {
  625.             // 如果要覆盖已经存在的目标文件,首先判断是否目标文件可写。
  626.             if (destFile.exists()) {
  627.                 if (!destFile.canWrite()) {
  628.                     throw new IOException("Cannot write the destination file: "
  629.                             + destFile.getAbsolutePath());
  630.                 }
  631.             } else {
  632.                 // 不存在就创建一个新的空文件。
  633.                 if (!destFile.createNewFile()) {
  634.                     throw new IOException("Cannot write the destination file: "
  635.                             + destFile.getAbsolutePath());
  636.                 }
  637.             }
  638.         }
  639.         BufferedInputStream inputStream = null;
  640.         BufferedOutputStream outputStream = null;
  641.         ZipOutputStream zipOutputStream = null;
  642.         byte[] block = new byte[1024];
  643.         try {
  644.             inputStream = new BufferedInputStream(new FileInputStream(srcFile));
  645.             outputStream = new BufferedOutputStream(new FileOutputStream(destFile));
  646.             zipOutputStream = new ZipOutputStream(outputStream);
  647.             
  648.             zipOutputStream.setComment("通过java程序压缩的");
  649.             ZipEntry  zipEntry = new ZipEntry(srcFile.getName());
  650.             zipEntry.setComment(" zipEntry通过java程序压缩的");
  651.             zipOutputStream.putNextEntry(zipEntry);
  652.             while (true) {
  653.                 int readLength = inputStream.read(block);
  654.                 if (readLength == -1)
  655.                     break;// end of file
  656.                 zipOutputStream.write(block, 0, readLength);
  657.             }
  658.             zipOutputStream.flush();
  659.             zipOutputStream.finish();
  660.         } finally {
  661.             if (inputStream != null) {
  662.                 try {
  663.                     inputStream.close();
  664.                 } catch (IOException ex) {
  665.                     // just ignore
  666.                 }
  667.             }
  668.             if (outputStream != null) {
  669.                 try {
  670.                     outputStream.close();
  671.                 } catch (IOException ex) {
  672.                     // just ignore
  673.                 }
  674.             }
  675.             if (zipOutputStream != null) {
  676.                 try {
  677.                     zipOutputStream.close();
  678.                 } catch (IOException ex) {
  679.                     // just ignore
  680.                 }
  681.             }           
  682.         }
  683.     }
  684.     /**
  685.      * @param args
  686.      * @throws IOException
  687.      */
  688.     public static void main(String[] args) throws IOException {
  689.         FileUtil.createDirs("d:/logs/aaaaaaaa/spring.log"false);
  690.         FileUtil.zipFile("d:/logs/复件 work.log"nulltrue);
  691.         System.out.println(getFileSeparator());
  692.         String[] temp = FileUtil.getLastLines(new File("d:/logs/work.log"), 5);
  693.         for (int i = 0; i < temp.length; i++) {
  694.             System.out.println(temp[i]);
  695.         }
  696.     }
  697. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值