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