package simple.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.junit.Test;
public class ApacheIO {
/**
* 用普通的Java IO来解析HTML <br>
*/
@Test
public void testReadURL1() throws MalformedURLException, IOException{
InputStream in = new URL( "http://www.blogjava.net/rongxh7" ).openStream();
try {
InputStreamReader inR = new InputStreamReader( in );
BufferedReader buf = new BufferedReader( inR );
String line;
while ( ( line = buf.readLine() ) != null ) {
System.out.println( line );
}
} finally {
in.close();
}
}
/**
* 用Apache IO来解析HTML <br>
* 学习要点: <br>
* IOUtils contains utility methods dealing with reading, writing and copying. <br>
* The methods work on InputStream, OutputStream, Reader and Writer.
*/
@Test
public void testReadURL2() throws MalformedURLException, IOException {
InputStream in = new URL("http://www.blogjava.net/rongxh7").openStream();
try {
System.out.println(IOUtils.toString(in));
} finally {
IOUtils.closeQuietly(in);
}
}
/**
* 用Apache IO来读文件<br>
* 学习要点:<br>
* The FileUtils class contains utility methods for working with File objects.<br>
* These include reading, writing, copying and comparing files.
*/
@Test
public void testReadFile() throws IOException {
File file = new File("README");
List<String> lines = FileUtils.readLines(file, "GBK");
for(String line : lines){
System.out.println(line);
}
}
/**
* 用Apache IO来操作文件名
* The FilenameUtils class contains utility methods <br>
* for working with filenames without using File objects.
*/
@Test
public void testFileName() {
String filename = "C:/a/b/ccc.txt";
String baseName = FilenameUtils.getBaseName(filename); //ccc
String extName = FilenameUtils.getExtension(filename); //txt
String fullPath = FilenameUtils.getFullPath(filename); //C:/a/b
String name = FilenameUtils.getName(filename); //ccc.txt
System.out.println(baseName);
System.out.println(extName);
System.out.println(fullPath);
System.out.println(name);
}
/**
* 用Apache IO查询磁盘空间
* The FileSystemUtils class contains utility methods <br>
* for working with the file system to access functionality not supported by the JDK.
*/
@Test
public void testFindDrive() throws IOException{
long space = FileSystemUtils.freeSpaceKb("C:/"); //查C盘还剩下多少可用空间
System.out.println("C盘可用空间为: " + space/1024 + " MB");
}
/**
* Line Iterator的用法 <br>
* The org.apache.commons.io.LineIterator class provides a flexible way <br>
* for working with a line-based file. An instance can be created directly, <br>
* or via factory methods on FileUtils or IOUtils.
*/
@Test
public void testLineIterater() throws IOException{
File file = new File("README");
LineIterator it = FileUtils.lineIterator(file, "GBK");
try{
while(it.hasNext()){
String line = it.nextLine();
System.out.println(line);
}
} finally {
LineIterator.closeQuietly(it);
}
}
/**
*
* 各种常用的FileFiter
*
* There are a number of 'primitive' filters:
*
* DirectoryFilter Only accept directories
* PrefixFileFilter Filter based on a prefix
* SuffixFileFilter Filter based on a suffix
* NameFileFilter Filter based on a filename
* WildcardFileFilter Filter based on wildcards
* AgeFileFilter Filter based on last modified time of file
* SizeFileFilter Filter based on file size
*
* And there are five 'boolean' filters:
*
* TrueFileFilter Accept all files
* FalseFileFilter Accept no files
* NotFileFilter Applies a logical NOT to an existing filter
* AndFileFilter Combines two filters using a logical AND
* OrFileFilter Combines two filter using a logical OR
*
*/
/**
* FileFilter的用法1
* 用 "new 子类" 的方式
*
*/
@Test
public void testFileFilter1(){
File dir = new File("data"); //若表示本目录,则用"."
//Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
String[] files = dir.list(
//Constructs a new file filter that ANDs the result of two other filters.
new AndFileFilter(
new AndFileFilter(
new PrefixFileFilter("t"), //Constructs a new Prefix file filter for a single prefix.
new OrFileFilter( //Constructs a new file filter that ORs the result of two other filters.
new SuffixFileFilter(".txt"), //Constructs a new Suffix file filter for a single extension.
new SuffixFileFilter(".dic")
)
),
//Constructs a new file filter that NOTs the result of another filters.
new NotFileFilter(
DirectoryFileFilter.INSTANCE //This filter accepts Files that are directories.
)
)
);
for ( int i=0; i<files.length; i++ ) {
System.out.println(files[i]);
}
}
/**
* FileFilter的用法2
* 用FileFilterUtils的方式
*/
@Test
public void testFileFilter2(){
File dir = new File("data");
String[] files = dir.list(
FileFilterUtils.andFileFilter(
FileFilterUtils.andFileFilter(
FileFilterUtils.prefixFileFilter("t"),
FileFilterUtils.orFileFilter(
FileFilterUtils.suffixFileFilter(".txt"),
FileFilterUtils.suffixFileFilter(".dic")
)
),
FileFilterUtils.notFileFilter(
FileFilterUtils.directoryFileFilter()
)
)
);
for ( int i=0; i<files.length; i++ ) {
System.out.println(files[i]);
}
}
}
commons IO examples
原创
©著作权归作者所有:来自51CTO博客作者sunxboy的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:FTP source code
下一篇:JPA技术简单介绍
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java IO - 源码: InputStream
本文主要从JDK 11 源码角度分析InputStream。
Java IO/NIO/AIO -
Java中commons-IO
目录一、概述二、导入 commons-io jar包1、获取 commons-io jar包2、导入 commons-io jar 包三、commons-io 的使用1、IO 工具类 FilenameUtils2、IO 工具类 FileUtils一、概述common
Java commons-IO FilenameUtils类 FileUtils类 jar -
apache commons io包快速入门
原文参考http://www.javacodegeeks.com/2014/10/apache-commons-io-tutorial.ht
java 操作系统 web.xml System apache -
server2025配置LDAP
Server配置解析简介 Server配置文件位于tomcat/conf/目录下,这个配置文件在tomcat启动的时候会使用Digester技术解析这个XML文件,创建一个Server实例。那么解读配置的时候,可以将这个配置文件看作一个对象,层级关系表示他们的从属关系。标签的属性就是对象的属性。1.&n
server2025配置LDAP server配置 Server apache 监听器