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;
)
)

分类:Java 时间:2010-03-29 人气:280
分享到:
blog comments powered by Disqus

相关文章

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp ...

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • [Essence] Java project to generate static pages 2010-12-11

    Be reproduced, reprinted, please indicate the source! This address: http://blog.csdn.net/xxd851116/archive/2009/06/24/4293239.aspx For the first time a project needs to generate static pages, many large cattle on the Internet to generate static Web p

  • Java method to generate static HMTL page summary 2011-01-09

    Technology solutions to generate static pages of reproduced Preface: This is a comprehensive program of static jsp dynamic page, the site posts the program will draw on the static methods in this post. To http://www.agilejava.org the single shared spirit

  • java access modifiers public protected friendly private usage summary 2009-04-01

    First, the statement: java in, friendly, and this modifier is not an explicit statement of the member variables and methods of the former would not have any modifiers, the default is friendly. For clarity, sub-summed up in three different circumstanc ...

  • Web content management platform using java technology generates a static html template 2010-07-17

    <html> <head> <title> # # # title # # # </ title> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> </ Head> <body> <table width="500" border="0" ali

  • java --- final keyword and static usage 2009-10-10

    final keyword and static usage 1, final According to the procedure context, Java keyword final there, "This can not be changed" or "end state" meaning, it can be modified non-abstract class, non-abstract class member methods and variab

  • java dynamic binding and static binding 2010-11-26

    package cn.lifx.test; public class Test { public static void whichFoo (Base arg1, Base arg2) { arg1.foo (arg2); } public static void main (String [] args) { Base b = new Base (); Derived d = new Derived (); whichFoo (b, b); whichFoo (b, d); whichFoo

  • Seen in the results to write Java programs: sub-class static method calls the parent class 2010-07-10

    1 === package test1; class A ( public static void print () ( System.out.println (A.class); / / here ) ) ------------------------------- class B extends A ( ) -------------------------------- public class Main ( public static void main (String [] args

  • Get the path where the java project 2010-03-29

    Get the path where the java project

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.640 (s). 12 q(s)