Java file operations enhancement tools

Java file operations enhancement tools

-------------------------------------------------- ------------------------------

2008-12-15 18:30:43 Tags: File IO commons [pushed to the technical ring]

Copyright Statement: original works, and if reproduced, please contact the author. Otherwise held accountable by law.
Java file operations enhancement tools

Java-based file operations too, the lack of many useful tools, such as the directory operation, to support the very good at all. If you often use Java operating a file or folder, you will find repeated the preparation of these codes is frustrating issue, but also to a large number of recursion is used.

Here is a solution, using Apache Commons IO toolkit to simple implementation file (folder) and copy, move, delete, get size of operation, has not been rigorously tested and found a problem please message to me.

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter .*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io. *;

/ **
* Files Toolbox
*
* @ Author leizhimin 2008-12-15 13:59:16
* /
public final class FileToolkit (
private static final Log log = LogFactory.getLog (FileToolkit.class);

/ **
* Copy files or directories, copy the file exactly the same as before.
*
* @ Param resFilePath source file path
* @ Param distFolder destination folder
* @ IOException thrown when an exception occurs when the operation
* /
public static void copyFile (String resFilePath, String distFolder) throws IOException (
File resFile = new File (resFilePath);
File distFile = new File (distFolder);
if (resFile.isDirectory ()) (
FileUtils.copyDirectoryToDirectory (resFile, distFile);
) Else if (resFile.isFile ()) (
FileUtils.copyFileToDirectory (resFile, distFile, true);
)
)

/ **
* Remove a file or directory
*
* @ Param targetPath file or directory path
* @ IOException thrown when an exception occurs when the operation
* /
public static void deleteFile (String targetPath) throws IOException (
File targetFile = new File (targetPath);
if (targetFile.isDirectory ()) (
FileUtils.deleteDirectory (targetFile);
) Else if (targetFile.isFile ()) (
targetFile.delete ();
)
)

/ **
* Mobile file or directory, moving around the file exactly the same, if the goal is to create a folder does not exist.
*
* @ Param resFilePath source file path
* @ Param distFolder destination folder
* @ IOException thrown when an exception occurs when the operation
* /
public static void moveFile (String resFilePath, String distFolder) throws IOException (
File resFile = new File (resFilePath);
File distFile = new File (distFolder);
if (resFile.isDirectory ()) (
FileUtils.moveDirectoryToDirectory (resFile, distFile, true);
) Else if (resFile.isFile ()) (
FileUtils.moveFileToDirectory (resFile, distFile, true);
)
)

/ **
* Rename the file or folder
*
* @ Param resFilePath source file path
* @ Param newFileName rename
* @ Return operation is successful identification
* /
public static boolean renameFile (String resFilePath, String newFileName) (
String newFilePath = StringToolkit.formatPath (StringToolkit.getParentPath (resFilePath) + "/" + newFileName);
File resFile = new File (resFilePath);
File newFile = new File (newFilePath);
return resFile.renameTo (newFile);
)

/ **
* Read the size of the file or directory
*
* @ Param distFilePath target file or folder
* @ Return the size of the file or directory, if the acquisition fails, then return -1
* /
public static long genFileSize (String distFilePath) (
File distFile = new File (distFilePath);
if (distFile.isFile ()) (
return distFile.length ();
) Else if (distFile.isDirectory ()) (
return FileUtils.sizeOfDirectory (distFile);
)
return-1L;
)

/ **
* Determine whether a file exists
*
* @ Param filePath the file path
* @ Return there to return true, otherwise returns false
* /
public static boolean isExist (String filePath) (
return new File (filePath). exists ();
)

/ **
* Local a list of files to a directory (not recursive)
*
* @ Param folder ftp a directory
* @ Param suffix file name suffix (eg. Mov.xml)
* @ Return the file name list
* /
public static String [] listFilebySuffix (String folder, String suffix) (
IOFileFilter fileFilter1 = new SuffixFileFilter (suffix);
IOFileFilter fileFilter2 = new NotFileFilter (DirectoryFileFilter.INSTANCE);
FilenameFilter filenameFilter = new AndFileFilter (fileFilter1, fileFilter2);
return new File (folder). list (filenameFilter);
)

/ **
* String to write to the specified file (when specified in the parent folder path does not exist, it will maximize to create in order to ensure a successful save!)
*
* @ Param res the original string
* @ Param filePath the file path
* @ Return the success tag
* /
public static boolean string2File (String res, String filePath) (
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try (
File distFile = new File (filePath);
if (! distFile.getParentFile (). exists ()) distFile.getParentFile (). mkdirs ();
bufferedReader = new BufferedReader (new StringReader (res));
bufferedWriter = new BufferedWriter (new FileWriter (distFile));
char buf [] = new char [1024]; / / character buffer
int len;
while ((len = bufferedReader.read (buf))! = -1) (
bufferedWriter.write (buf, 0, len);
)
bufferedWriter.flush ();
bufferedReader.close ();
bufferedWriter.close ();
) Catch (IOException e) (
flag = false;
e.printStackTrace ();
)
return flag;
)
)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Java file operations enhancement tools

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

  • spring struts2.0 hibernate bug killer 1

    exception There is no Action mapped for namespace / and action name checkLogin. - [Unknown location] com.opensymphony.xwork2.DefaultActionProxy.prepare (DefaultActionProxy.java: 186) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries