Apache Commons IO overview

Source: Internet
Author: User

First, post an overview of Apache Commons IO official website

Commons IO 2.4 API

Packages

Org.apache.commons.io

This package defines utility classes for working with streams, readers, writers and files.

Org.apache.commons.io.comparator

This is provides various Comparator implementations for File S.

Org.apache.commons.io.filefilter

This package defines an interface (Iofilefilter) that combines both FileFilter and FilenameFilter .

Org.apache.commons.io.input

This package provides implementations of input classes, such as InputStream and Reader .

Org.apache.commons.io.monitor

This is provides a component for monitoring file system events (directory and file Create, update and delete events).

Org.apache.commons.io.output

This package provides implementations of output classes, such as OutputStream and Writer .

The times are growing fast, time is going faster, and my eclipse has a lot of Commons IO or 1.2 versions. Now the mainstream version of Commons is 2.4.

Write some code to warm up first.

Package Test.ffm83.commons.io;

importjava.io.File;

importJava.io.FileInputStream;

importJava.io.FileWriter;

importjava.io.IOException;

importJava.io.InputStream;

importJava.io.Writer;

importJava.net.URL;

importorg.apache.commons.io.FileUtils;

importorg.apache.commons.io.IOUtils;

importorg.apache.commons.lang.StringUtils;

/**

* Some simple basic usage of Commonsio

* @author Fan Fangming

*/

Public class Iobaseusage {

publicstatic void main (string[] args) throws Exception {

Getinputcopytowrite ();

Writestringtofile ();

Geturlwritetofile ();

}

/**

* Input stream copied to output stream

* @author Fan Fangming

*/

privatestatic void Getinputcopytowrite () throws Exception {

Inputstreamins = new fileinputstream (new File ("D:\\ffm83\\ffm83.txt"));

Writerwrite = new FileWriter ("D:\\ffm83\\write.txt");

Try {

Ioutils. Copy (Ins,write);

System. out. println (StringUtils. Center("input stream copied to output stream succeeded", 50, "-"));

}catch(IOException e) {

System. out. println (StringUtils. Center("input stream failed to copy to output stream", 50, "-"));

E.printstacktrace ();

}

Write.close ();

Ins.close ();

}

/**

* Character output to specified text

* @author Fan Fangming

*/

privatestatic void Writestringtofile () throws Exception {

STRINGSTR = "character output to the specified text, author Fan Fangming. ";

Filefile = new File ("D:\\ffm83\\ffm83.txt");

Try {

FileUtils. Writestringtofile (File,str, "UTF-8");

System. out. println (StringUtils. Center("character output to specified text success", 50, "-"));

}catch(IOException e) {

System. out. println (StringUtils. Center("character output to specified text failed", 50, "-"));

E.printstacktrace ();

}

}

/**

* Get URL stream, convert to string

* @author Fan Fangming

*/

privatestatic void geturlwritetofile () throws Exception {

Urlurl = new URL ("http://blog.csdn.net/ffm83/article/details/42000885");

Inputstreamips = Url.openstream ();

Stringurlcontent = "";

Try {

Urlcontent= ioutils. toString (IPs, "UTF-8");

System. out. println (urlcontent);//You can also use Writestringtofile to retain text

System. out. println (StringUtils. Center("Get URL stream, convert to string Success", 50, "-"));

}catch(IOException e) {

System. out. println (StringUtils. Center("Get URL stream, convert to string failed", 50, "-"));

E.printstacktrace ();

}finally{

Ioutils. closequietly (IPS);

}

}

}

Apache Commons IO overview

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.