处理IO的一些Java工具类包:Commons-IO
jopen
13年前
Commons项目中用来处理IO的一些工具类包。 <br /> <p style="line-height:normal;margin-bottom:12pt;" class="MsoNormal"><span>Commons IO is a library of utilities to assist with developing IO functionality. There are four main areas included: <br /> <br /> ●Utility classes - with static methods to perform common tasks <br /> ●Filters - various implementations of file filters <br /> ●Comparators - various implementations of java.util.Comparator for files<br /> ●Streams - useful stream, reader and writer implementations</span></p> <p></p> <table style="width:100%;" class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" width="100%"> <tbody> <tr> <td colspan="2"> <p style="line-height:normal;" class="MsoNormal"><strong><span style="font-family:'Times New Roman','serif';font-size:18pt;">Packages</span></strong></p> </td> </tr> <tr> <td width="20%"> <p style="line-height:normal;" class="MsoNormal"><strong><span style="font-family:'Times New Roman','serif';font-size:12pt;"><a href="/misc/goto?guid=4959499953255140806"><span style="color:#0000ff;">org.apache.commons.io</span></a></span></strong></p> </td> <td> <p style="line-height:normal;" class="MsoNormal"><span style="font-family:'Times New Roman','serif';font-size:12pt;">This package defines utility classes for working with streams, readers, writers and files.</span></p> </td> </tr> <tr> <td width="20%"> <p style="line-height:normal;" class="MsoNormal"><strong><span style="font-family:'Times New Roman','serif';font-size:12pt;"><a href="/misc/goto?guid=4959499953333836232"><span style="color:#0000ff;">org.apache.commons.io.comparator</span></a></span></strong></p> </td> <td> <p style="line-height:normal;" class="MsoNormal"><span style="font-family:'Times New Roman','serif';font-size:12pt;">This package provides various <a title="class or interface in java.util" href="/misc/goto?guid=4959499953420364957"><span style="font-family:'Courier New';color:#0000ff;font-size:10pt;">Comparator</span></a> implementations for <a title="class or interface in java.io" href="/misc/goto?guid=4959499953498056724"><span style="font-family:'Courier New';color:#0000ff;font-size:10pt;">File</span></a>s.</span></p> </td> </tr> <tr> <td width="20%"> <p style="line-height:normal;" class="MsoNormal"><strong><span style="font-family:'Times New Roman','serif';font-size:12pt;"><a href="/misc/goto?guid=4959499953583571102"><span style="color:#0000ff;">org.apache.commons.io.filefilter</span></a></span></strong></p> </td> <td> <p style="line-height:normal;" class="MsoNormal"><span style="font-family:'Times New Roman','serif';font-size:12pt;">This package defines an interface (IOFileFilter) that combines both <a title="class or interface in java.io" href="/misc/goto?guid=4959499953667863391"><span style="font-family:'Courier New';color:#0000ff;font-size:10pt;">FileFilter</span></a> and <a title="class or interface in java.io" href="/misc/goto?guid=4959499953739751985"><span style="font-family:'Courier New';color:#0000ff;font-size:10pt;">FilenameFilter</span></a>.</span></p> </td> </tr> <tr> <td width="20%"> <p style="line-height:normal;" class="MsoNormal"><strong><span style="font-family:'Times New Roman','serif';font-size:12pt;"><a href="/misc/goto?guid=4959499953822759343"><span style="color:#0000ff;">org.apache.commons.io.input</span></a></span></strong></p> </td> <td> <p style="line-height:normal;" class="MsoNormal"><span style="font-family:'Times New Roman','serif';font-size:12pt;">This package provides implementations of input classes, such as </span><span style="font-family:'Courier New';font-size:10pt;">InputStream</span><span style="font-family:'Times New Roman','serif';font-size:12pt;"> and </span><span style="font-family:'Courier New';font-size:10pt;">Reader</span><span style="font-family:'Times New Roman','serif';font-size:12pt;">.</span></p> </td> </tr> <tr> <td width="20%"> <p style="line-height:normal;" class="MsoNormal"><strong><span style="font-family:'Times New Roman','serif';font-size:12pt;"><a href="/misc/goto?guid=4959499953896992894"><span style="color:#0000ff;">org.apache.commons.io.output</span></a></span></strong></p> </td> <td> <p style="line-height:normal;" class="MsoNormal"><span style="font-family:'Times New Roman','serif';font-size:12pt;">This </span></p> </td> </tr> </tbody> </table> <br /> 下面是一些示例代码: <pre class="brush:java; toolbar: true; auto-links: false;">//直接将IO流转成字符串 InputStream in = new URL( "http://jakarta.apache.org" ).openStream(); try { System.out.println( IOUtils.toString( in ) ); } finally { IOUtils.closeQuietly(in); } //读取文本文件的所有行 File file = new File("/commons/io/project.properties"); List lines = FileUtils.readLines(file, "UTF-8"); //路径处理 String filename = "C:/commons/io/../lang/project.xml"; String normalized = FilenameUtils.normalize(filename); // result is "C:/commons/lang/project.xml" //获取目录空间 long freeSpace = FileSystemUtils.freeSpace("C:/"); //打印文件的所有行 LineIterator it = FileUtils.lineIterator(file, "UTF-8"); try { while (it.hasNext()) { String line = it.nextLine(); /// do something with line } } finally { LineIterator.closeQuietly(iterator); }</pre> <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1324994173561" target="_blank">http://www.open-open.com/lib/view/home/1324994173561</a></p>