Apache commons io 概要介绍
首先贴一段Apache commons IO官网上的概要介绍
Commons IO 2.4 API
Packages |
|
This package defines utility classes for working with streams, readers, writers and files. |
|
This package provides various |
|
This package defines an interface (IOFileFilter) that combines both |
|
This package provides implementations of input classes, such as |
|
This package provides a component for monitoring file system events (directory and file create, update and delete events). |
|
This package provides implementations of output classes, such as |
时代发展很快,时间过的更快,我eclipse里面很多的commons io还是1.2版本。现在commons的主流版本是2.4.
先写点代码热热身。
packagetest.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;
/**
* commonsio 的一些简单基本用法
* @author范芳铭
*/
public classIOBaseUsage {
publicstatic void main(String[] args) throws Exception {
//getInputCopyToWrite();
//writeStringToFile();
getUrlWriteToFile();
}
/**
* 输入流复制到输出流
* @author 范芳铭
*/
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("输入流复制到输出流成功",50, "-"));
}catch(IOException e){
System.out.println(StringUtils.center("输入流复制到输出流失败",50, "-"));
e.printStackTrace();
}
write.close();
ins.close();
}
/**
* 字符输出到指定文本
* @author 范芳铭
*/
privatestatic void writeStringToFile() throws Exception {
Stringstr = "字符输出到指定文本,作者范芳铭。";
Filefile = new File("D:\\ffm83\\ffm83.txt");
try{
FileUtils.writeStringToFile(file,str,"UTF-8");
System.out.println(StringUtils.center("字符输出到指定文本成功",50, "-"));
}catch(IOException e){
System.out.println(StringUtils.center("字符输出到指定文本失败",50, "-"));
e.printStackTrace();
}
}
/**
* 获取url流,转换成String
* @author 范芳铭
*/
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);//也可以利用writeStringToFile保留到文本
System.out.println(StringUtils.center("获取url流,转换成String成功",50, "-"));
}catch(IOException e){
System.out.println(StringUtils.center("获取url流,转换成String失败",50, "-"));
e.printStackTrace();
}finally{
IOUtils.closeQuietly(ips);
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。