Common methods of Apache Commons io fileutils

Source: Internet
Author: User

Apache Commons IO

In the study of the IO stream when studying (translated) this, only fileutils some of the methods, not comprehensive, please understand

Org.apache.commons.io

This package defines a utility class based on streams, readers, writers and files.

Org.apache.commons.io.comparator

This package defines the implementation of a variety of file-based comparators

Org.apache.commons.io.filefilter

This package defines an interface (Iofielfieldfield) that incorporates FieldField1 and Fielnamefield.

Org.apache.commons.io.input

This package provides an implementation of the input class, such as an input stream and a stream of read characters.

Org.apache.commons.io.monitor

This package provides a component to monitor file system events (directory and file creation, update, and delete events).

Org.apache.commons.io.output

This package provides an implementation of the output class, such as an output stream and a write stream

Org.apache.commons.io.serialization

This package provides a framework to control the deserialization of classes.

The common methods in Fileutils
Package Com.wzlove.commons;import Org.apache.commons.io.fileutils;import Org.junit.test;import java.io.File;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.net.URL;import Java.nio.charset.charset;import Java.util.arraylist;import Java.util.collection;import java.util.Collections; Import java.util.list;/** * Test Fileutiles Common Methods * * @author Wzlove * @create 2018-07-21 13:29 */public class Fileutilsdemo {/** * static void Cleandirectory (File directory) * Deletes all contents within a folder, but does not delete this folder */@Test public void TESTC        Leandirectory () throws IOException {//Note that the virtual path of the created folder is file dir = new file ("F:\\ddd\\aaa");    Fileutils.cleandirectory (dir); }/** * Static Boolean contentequals (file file1, file file2) * Compares two files for the same content, returns true differently, and returns False (. Note that two files do not exist and will Returns True) */@Test public void Testcontentequals () throws IOException {file File1 = new file ("F:\\ddd\\aaa        \\a.txt "); File file2 = New File ("F:\\ddd\\aaa\\b.txt");    System.out.println (Fileutils.contentequals (file1,file2));     }/** * Static file[] Convertfilecollectiontofilearray (collection<file> files) * Convert the collection to an array */@Test        public void Testconvertfilecollectiontofilearray () {arraylist<file> fileList = new arraylist<> ();        Filelist.add (New File ("C:\\aa"));        Filelist.add (New File ("D:\\aa"));        Filelist.add (New File ("E:\\aa"));        Filelist.add (New File ("F:\\aa"));        file[] files = Fileutils.convertfilecollectiontofilearray (fileList);        for (int i = 0; i < files.length; i++) {System.out.println (Files[i].getabsolutepath ());     }}/** * static void CopyDirectory (file srcdir, filename destDir) * Copy files under Srcdir directory to DestDir directory, file exists and not duplicated * There are many overloaded methods that can pass file filters and so on */@Test public void Testcopydirectory () throws IOException {file Srcdir = new F        Ile ("F:\\aaa"); File DestDir = new file ("F:\\bbB ");    Fileutils.copydirectory (Srcdir,destdir);    }/** * static void Copydirectorytodirectory (file srcdir, file destDir) * Place srcdir entire folder under DestDir folder */        @Test public void Testcopydirectorytodirectory () throws IOException {file Srcdir = new File ("f:\\aaa");        File DestDir = new file ("f:\\bbb");    Fileutils.copydirectorytodirectory (Srcdir,destdir);    }/** * static void CopyFile (file srcfile, file destfile) * Copy srcfile file to DestFile, DestFile does not exist the target file will be created */        @Test public void Testcopyfile () throws IOException {file Srcdir = new File ("F:\\aaa\\1.txt");        File DestDir = new file ("F:\\bbb\\a.txt");    Fileutils.copyfile (Srcdir,destdir);     }/** * static void Copyfiletodirectory (file srcfile, file destDir) * Copy the Srcfile file to the DestDir directory */@Test        public void Testcopyfiletodirectory () throws IOException {file Srcdir = new File ("F:\\aaa\\1.txt"); File DestDir = new file ("F:\\BBB");    Fileutils.copyfiletodirectory (Srcdir,destdir); }/** * static void Copytodirectory (file src, file destDir) * Copy files or folders to the target folder */@Test public void        Testcopytodirectory () throws IOException {file Srcdir = new file ("F:\\aaa");        File DestDir = new file ("f:\\bbb");    Fileutils.copytodirectory (Srcdir,destdir); }/** * static void Copyurltofile (URL source, file destination) * Copies bytes from the URL of source to a File D     Estination. */@Test public void Testcopyurltofile () throws IOException {File Destination = new file ("F:\\bbb\\baidu.txt        ");    Fileutils.copyurltofile (New URL ("Http://ntlias-stu.boxuegu.com/#/index"), destination); }/** * static void DeleteDirectory (file directory) * Delete this folder, delete all, including this file, and Cleandirectory method to make a comparison */@Tes        T public void Testdeletedirectory () throws IOException {file directory = new file ("F:\\BBB\\CCC"); Fileutils.deletedirectory (directory);    }/** * Static Boolean deletequietly (file file) * Delete this files, do not throw exception */@Test public void Testdelete        Quietly () {file directory = new file ("F:\\bbb\\a.txt");    System.out.println (fileutils.deletequietly (directory)); }/** * static void Forcemkdir (File directory) * Create folders, you can create multi-level folders */@Test public void Testforcemkdir        () throws IOException {file directory = new file ("F:\\bbb\\ccc\\ddd\\aaa");    Fileutils.forcemkdir (directory); /** * static void forcemkdirparent (file file) * Creates a parent path for a file, but does not create this file (strange) */@Test public void Te        Stforcemkdirparent () throws IOException {file directory = new file ("F:\\bbb\\aaa\\ccc\\a.txt");    Fileutils.forcemkdirparent (directory); }/** * static String Getuserdirectorypath () * Returns the user's home directory */@Test public void Testgetuserdirectorypath        () {String str = Fileutils.getuserdirectorypath ();    System.out.println (str); }    /**    * static void MoveDirectory (file srcdir, file destDir) * Modifies the file name, changes the file name of Srcdir.getname () to Destdir.getname (), if the file name exists,        Error */@Test public void Testmovedirectory () throws IOException {file Srcdir = new file ("F:\\aaa");        File DestDir = new file ("F:\\CCC");    Fileutils.movedirectory (Srcdir,destdir); }/** * static void Movedirectorytodirectory (file src, file DestDir, Boolean createdestdir) * moves src to DestDir, The following Boolean indicates whether the folder is created when the Destdir folder does not exist, false is not created, true is created * If SRC does not have an error, if SRC already exists in Destdir, then the error */@Test public VO        ID testmovedirectorytodirectory () throws IOException {file Srcdir = new File ("f:\\bbb");        File DestDir = new file ("F:\\eee");    Fileutils.movedirectorytodirectory (srcdir,destdir,true); }/** * static void MoveFile (file srcfile, file destfile) * Modified file name * static void Movefiletodirectory (file Srcfile, File DestDir, Boolean createdestdir) * Move a file to a folder, and the last parameter is true to create if DestDir does not exist,False to not create * static void Movetodirectory (file src, file DestDir, Boolean createdestdir) * Move file or folder to destination folder, last parameter is T     Rue indicates that if Destdir is not present, it is created, false does not create *//** * static FileInputStream openinputstream (file file) * Returns a byte input stream        */@Test public void Testopeninputstream () throws IOException {File File = new file ("F:\\bbb\\1.txt");        FileInputStream FIS = fileutils.openinputstream (file);    A byte stream can be read operation Fis.close ();      }/** * Static FileOutputStream openoutputstream (file file) * Returns the byte output stream, creates a multilevel directory, directory exists, clears the contents of the file if many files and directories do not exist *//** * static FileOutputStream openoutputstream (file File, Boolean append) * Returns the byte output stream, if the file is many and the directory does not exist, create a multilevel directory if     Directory exists, will not empty the content, output to the file, will be appended to the original content * * */** * static byte[] Readfiletobytearray (file file) * Read file, return byte array        */@Test public void Testreadfiletobytearray () throws IOException {File File = new file ("F:\\bbb\\1.txt"); byte[] bytes = Fileutils.readfiletObytearray (file);    System.out.println (New String (bytes)); }/** * static string readfiletostring (file file, string encoding) * reads files in the specified encoding format */@Test public        void Testreadfiletostring () throws IOException {File File = new file ("F:\\bbb\\1.txt");        String str = fileutils.readfiletostring (file, "UTF-8");    System.out.println (str);     }/** * Static list<string> readlines (file file, string encoding) * Reads files according to the specified encoding, returns a String collection, each row of the file is an element        */@Test public void Testreadlines () throws IOException {File File = new file ("F:\\bbb\\1.txt");        list<string> list = fileutils.readlines (file, "UTF-8");        for (String s:list) {System.out.println (s); }}/** * static long sizeOf (file file) * Returns the size of files or folders */@Test public void testsizeof () throws I        oexception {File File = new file ("F:\\bbb\\1.txt");        Long L = fileutils.sizeof (file); System.out.println (l);        File File1 = new file ("f:\\bbb");    System.out.println (fileutils.sizeof (file1));     }/** * static file tofile (URL url) * Convert from a URL to a file. * Not understood */@Test public void Testtofile () throws IOException {File File = Fileutils.tofile ("file:\        \F:\\BBB "));    System.out.println (File.getabsolutepath ()); }/** * static void Writebytearraytofile (file file, byte[] data) * Writes a byte array to the file, if the destination file does not exist, the target file is created, the file exists to empty the contents of the file, and then in Line input * static void Writebytearraytofile (file file, byte[] data, Boolean append) * writes a byte array to the file, if the destination file does not exist, the target file is created, the file exists and the last parameter is true, appends the content to the original content * static void Writebytearraytofile (file file, byte[] data, int off, int len) * will byte An array of type data, starting from the off index, the content of Len is output to the specified file, if the destination file does not exist, the target file is created, the file exists, and the contents of the empty file are entered. * static void Writebytearraytofile (file File, byte[] data, int off, int len, Boolean append) * The byte type array data, starting from the off index, Len-length content is output to the specified file, if the destination file does not exist, the target file is created, exists and the last parameter is TruE, appends the content to the original content * static void Writestringtofile (file file, string data, string encoding) * Writes the data of type String in the specified encoding format Into the file, if the destination file does not exist, the target file is created, the file is empty, and then entered * static void Writestringtofile (file file, string data, string encoding, bo Olean Append) * Writes a string type of data to a file in the specified encoding format, if the destination file does not exist, the destination file is created, the file exists and the last parameter is true, and the contents are appended to the original content *//** * stat     IC void writelines (file file, collection<?> lines) * Writes collection data to a file, each data occupies one row, if the target file does not exist, the target file is created, the file exists to empty the contents of the file, and then input * static void Writelines (file file, collection<?> lines, Boolean append) * Writes collection data to a file, each data occupies one line, */@Tes        T public void Testwritelines () throws IOException {File File = new file ("F:\\ddd\\a.txt");        arraylist<string> coll = new arraylist<> ();        Coll.add ("I love Java");        Coll.add ("I Love You");    Fileutils.writelines (File,coll); }/** * static void Writelines (file file, collection<?> lines, String lineending) * All collection data is saved in one row and isSeparated, that is, each collection of data will be followed by a lineending string, if the target file does not exist, * will create the target file, the file exists to empty the contents of the file, and then enter * static void Writelines (file file, Collect     Ion<?> lines, String lineending, Boolean append) * All collection data is saved on one line and is spaced, that is, each collection of data is followed by a lineending string. If the destination file does not exist, * The target file is created, the file exists and the last parameter is true, appending the contents to the back of the original content */@Test public void TesWriteLines2 () throws IOException {Fi        Le file = new file ("F:\\ddd\\a.txt");        arraylist<string> coll = new arraylist<> ();        Coll.add ("I love Java");        Coll.add ("I love Java");        Coll.add ("I love Java");    Fileutils.writelines (File,coll, "I Love You"); }/** * static void Writelines (file file, String encoding, collection<?> lines) * Writes the collection data in encoding encoded format to file, each data occupies a row, if the target file does not exist, * will create the target file, the file exists empty file contents, and then input * static void Writelines (file file, String encoding, COLLECTI On<?> lines, Boolean append) * Writes the collection data to a file in encoding encoded format, each data occupies one row, if the target file does not exist, * The target file is created, the file exists and the last parameter is true, the    Append content to the back of the original content */@Test public void TesWriteLines3 () throws IOException {File File = new file ("F:\\ddd\\a.txt");        arraylist<string> coll = new arraylist<> ();        Coll.add ("I love Java");        Coll.add ("I love Java");        Coll.add ("I love Java");    Fileutils.writelines (file, "UTF-8", coll); }/** * static void Writelines (file file, string encoding, collection<?> lines, String lineending) * by E Ncoding encoded format writes the collection data to a file, all collection data is saved on one line, and is spaced, that is, each set of data will be followed by a * lineending string, if the target file does not exist, the target file will be created, the file exists empty file contents, and then input * s tatic void writelines (file file, string encoding, collection<?> lines, String lineending, Boolean append) * by E Ncoding encoded format writes the collection data to a file, all collection data is saved on one line, and is spaced, that is, each set of data is followed by a * lineending string, if the target file does not exist, the target file is created, the file exists and the last parameter is true, The content is appended to the original content */@Test public void TesWriteLines4 () throws IOException {File File = new file ("F:\\ddd\\a        . txt ");    arraylist<string> coll = new arraylist<> ();    Coll.add ("I love Java");        Coll.add ("I love Java");        Coll.add ("I love Java");    Fileutils.writelines (file, "UTF-8", coll, "I Love You"); }}

Apache Commons io fileutils Common methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.