当前位置: 首页 > news >正文

兰州网站制作服务电话新手学做网站用什么软件

兰州网站制作服务电话,新手学做网站用什么软件,万能建站网站,女装网站建设的规划书Apache Commons IO是由Apache Foundation创建和维护的Java库。 它提供了许多类#xff0c;使开发人员可以轻松地完成常见任务#xff0c;并且减少样板代码 #xff0c;而每个项目都需要一遍又一遍地编写此类库的重要性是巨大的#xff0c;因为它们已经成熟由经验丰富的开发… Apache Commons IO是由Apache Foundation创建和维护的Java库。 它提供了许多类使开发人员可以轻松地完成常见任务并且减少样板代码 而每个项目都需要一遍又一遍地编写此类库的重要性是巨大的因为它们已经成熟由经验丰富的开发人员进行维护 他们已经考虑了每种可能的情况或者修复了各种错误。 在此示例中我们将根据功能所属的org.apache.commons.io包介绍一些具有不同功能的方法。 我们不会在库中深入研究因为它巨大但是我们将提供一些常见用法的示例这些示例对于每个开发人员无论初学者或不入门都可以派上用场。 1. Apache Commons IO示例 该示例的代码将分为几个类并且每个类都代表Apache Commons IO涵盖的特定领域。 这些区域是 实用程序类 输入项 输出量 筛选器 比较器 文件监控 为了使事情更清楚我们将输出分成多个块 每个创建的类一个。 我们还在项目文件夹名为ExampleFolder 内创建了一个目录其中包含将在此示例中使用的各种文件以显示各种类的功能。 注意为了使用org.apache.commons.io 您需要下载jar文件在此处找到并通过右键单击项目文件夹- Build Path-将它们添加到Eclipse项目的构建路径中。添加外部档案。 ApacheCommonsExampleMain.java public class ApacheCommonsExampleMain {public static void main(String[] args) {UtilityExample.runExample();FileMonitorExample.runExample();FiltersExample.runExample();InputExample.runExample();OutputExample.runExample();ComparatorExample.runExample();} } 这是将用于运行示例中其他类的方法的主要类。 您可以注释某些类以查看所需的输出。 1.1实用程序类 包org.apache.commons.io内有各种实用程序类其中大多数与文件操作和字符串比较有关。 我们在这里使用了一些最重要的方法 FilenameUtils 此类具有使用文件名的方法主要要点是使每个OS的工作更轻松在Unix和Windows系统中同样有效。 FileUtils 它提供用于文件操作 移动打开和读取文件检查文件是否存在等的方法。 IOCase 字符串操作和比较方法。 FileSystemUtils 其方法返回指定驱动器的可用空间。 UtilityExample.java import java.io.File; import java.io.IOException;import org.apache.commons.io.FileSystemUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.LineIterator; import org.apache.commons.io.IOCase;public final class UtilityExample {// We are using the file exampleTxt.txt in the folder ExampleFolder,// and we need to provide the full path to the Utility classes.private static final String EXAMPLE_TXT_PATH C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleTxt.txt;private static final String PARENT_DIR C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample;public static void runExample() throws IOException {System.out.println(Utility Classes example...);// FilenameUtilsSystem.out.println(Full path of exampleTxt: FilenameUtils.getFullPath(EXAMPLE_TXT_PATH));System.out.println(Full name of exampleTxt: FilenameUtils.getName(EXAMPLE_TXT_PATH));System.out.println(Extension of exampleTxt: FilenameUtils.getExtension(EXAMPLE_TXT_PATH));System.out.println(Base name of exampleTxt: FilenameUtils.getBaseName(EXAMPLE_TXT_PATH));// FileUtils// We can create a new File object using FileUtils.getFile(String)// and then use this object to get information from the file.File exampleFile FileUtils.getFile(EXAMPLE_TXT_PATH);LineIterator iter FileUtils.lineIterator(exampleFile);System.out.println(Contents of exampleTxt...);while (iter.hasNext()) {System.out.println(\t iter.next());}iter.close();// We can check if a file exists somewhere inside a certain directory.File parent FileUtils.getFile(PARENT_DIR);System.out.println(Parent directory contains exampleTxt file: FileUtils.directoryContains(parent, exampleFile));// IOCaseString str1 This is a new String.;String str2 This is another new String, yes!;System.out.println(Ends with string (case sensitive): IOCase.SENSITIVE.checkEndsWith(str1, string.));System.out.println(Ends with string (case insensitive): IOCase.INSENSITIVE.checkEndsWith(str1, string.));System.out.println(String equality: IOCase.SENSITIVE.checkEquals(str1, str2));// FileSystemUtilsSystem.out.println(Free disk space (in KB): FileSystemUtils.freeSpaceKb(C:));System.out.println(Free disk space (in MB): FileSystemUtils.freeSpaceKb(C:) / 1024);} } 输出量 Utility Classes example... Full path of exampleTxt: C:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\ Full name of exampleTxt: exampleTxt.txt Extension of exampleTxt: txt Base name of exampleTxt: exampleTxt Contents of exampleTxt...This is an example text file.We will use it for experimenting with Apache Commons IO. Parent directory contains exampleTxt file: true Ends with string (case sensitive): false Ends with string (case insensitive): true String equality: false Free disk space (in KB): 32149292 Free disk space (in MB): 31395 1.2文件监控器 org.apache.commons.io.monitor软件包包含可以获取有关文件的特定信息的方法但更重要的是它可以创建可用于跟踪特定文件或文件夹中的更改并根据更改执行操作的处理程序。 。 让我们看一下代码 FileMonitorExample.java import java.io.File; import java.io.IOException;import org.apache.commons.io.FileDeleteStrategy; import org.apache.commons.io.FileUtils; import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; import org.apache.commons.io.monitor.FileAlterationMonitor; import org.apache.commons.io.monitor.FileAlterationObserver; import org.apache.commons.io.monitor.FileEntry;public final class FileMonitorExample {private static final String EXAMPLE_PATH C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleFileEntry.txt;private static final String PARENT_DIR C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder;private static final String NEW_DIR C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\newDir;private static final String NEW_FILE C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\newFile.txt;public static void runExample() {System.out.println(File Monitor example...);// FileEntry// We can monitor changes and get information about files// using the methods of this class.FileEntry entry new FileEntry(FileUtils.getFile(EXAMPLE_PATH));System.out.println(File monitored: entry.getFile());System.out.println(File name: entry.getName());System.out.println(Is the file a directory?: entry.isDirectory());// File Monitoring// Create a new observer for the folder and add a listener// that will handle the events in a specific directory and take action.File parentDir FileUtils.getFile(PARENT_DIR);FileAlterationObserver observer new FileAlterationObserver(parentDir);observer.addListener(new FileAlterationListenerAdaptor() {Overridepublic void onFileCreate(File file) {System.out.println(File created: file.getName());}Overridepublic void onFileDelete(File file) {System.out.println(File deleted: file.getName());}Overridepublic void onDirectoryCreate(File dir) {System.out.println(Directory created: dir.getName());}Overridepublic void onDirectoryDelete(File dir) {System.out.println(Directory deleted: dir.getName());}});// Add a monior that will check for events every x ms,// and attach all the different observers that we want.FileAlterationMonitor monitor new FileAlterationMonitor(500, observer);try {monitor.start();// After we attached the monitor, we can create some files and directories// and see what happens!File newDir new File(NEW_DIR);File newFile new File(NEW_FILE);newDir.mkdirs();newFile.createNewFile();Thread.sleep(1000);FileDeleteStrategy.NORMAL.delete(newDir);FileDeleteStrategy.NORMAL.delete(newFile);Thread.sleep(1000);monitor.stop();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}} } 输出量 File Monitor example... File monitored: C:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\exampleFileEntry.txt File name: exampleFileEntry.txt Is the file a directory?: false Directory created: newDir File created: newFile.txt Directory deleted: newDir File deleted: newFile.txt 让我们看看这里发生了什么。 我们使用了org.apache.commons.io.monitor包中的某些类这些类使我们能够创建侦听特定事件的处理程序 在本例中该处理程序与文件文件夹目录等有关。 为了实现这一点需要采取某些步骤 创建一个File对象该对象是我们要侦听更改的目录的引用。 创建一个FileAlterationObserver对象该对象将观察这些更改。 使用addListener()方法将FileAlterationListenerAdaptor添加到观察器。 您可以使用多种方法来创建适配器但是在我们的示例中我们使用了一个嵌套类该类仅实现某些方法示例要求所需要的方法。 创建一个FileAlterationMonitor并添加您拥有的观察者以及间隔以毫秒为单位。 使用start()方法启动监视器并在必要时使用stop()方法将其stop() 。 1.3过滤器 过滤器可以多种组合方式使用 。 他们的工作是使我们能够轻松区分文件并获得满足特定条件的文件。 我们还可以结合使用过滤器来执行逻辑比较并更精确地获取文件而无需在以后使用繁琐的字符串比较。 FiltersExample.java import java.io.File;import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.AndFileFilter; import org.apache.commons.io.filefilter.NameFileFilter; 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.apache.commons.io.filefilter.WildcardFileFilter;public final class FiltersExample {private static final String PARENT_DIR C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder;public static void runExample() {System.out.println(File Filter example...);// NameFileFilter// Right now, in the parent directory we have 3 files:// directory example// file exampleEntry.txt// file exampleTxt.txt// Get all the files in the specified directory// that are named example.File dir FileUtils.getFile(PARENT_DIR);String[] acceptedNames {example, exampleTxt.txt};for (String file: dir.list(new NameFileFilter(acceptedNames, IOCase.INSENSITIVE))) {System.out.println(File found, named: file);}//WildcardFileFilter// We can use wildcards in order to get less specific results// ? used for 1 missing char// * used for multiple missing charsfor (String file: dir.list(new WildcardFileFilter(*ample*))) {System.out.println(Wildcard file found, named: file);}// PrefixFileFilter // We can also use the equivalent of startsWith// for filtering files.for (String file: dir.list(new PrefixFileFilter(example))) {System.out.println(Prefix file found, named: file);}// SuffixFileFilter// We can also use the equivalent of endsWith// for filtering files.for (String file: dir.list(new SuffixFileFilter(.txt))) {System.out.println(Suffix file found, named: file);}// OrFileFilter // We can use some filters of filters.// in this case, we use a filter to apply a logical // or between our filters.for (String file: dir.list(new OrFileFilter(new WildcardFileFilter(*ample*), new SuffixFileFilter(.txt)))) {System.out.println(Or file found, named: file);}// And this can become very detailed.// Eg, get all the files that have ample in their name// but they are not text files (so they have no .txt extension.for (String file: dir.list(new AndFileFilter( // we will match 2 filters...new WildcardFileFilter(*ample*), // ...the 1st is a wildcard...new NotFileFilter(new SuffixFileFilter(.txt))))) { // ...and the 2nd is NOT .txt.System.out.println(And/Not file found, named: file);}} } 输出量 File Filter example... File found, named: example File found, named: exampleTxt.txt Wildcard file found, named: example Wildcard file found, named: exampleFileEntry.txt Wildcard file found, named: exampleTxt.txt Prefix file found, named: example Prefix file found, named: exampleFileEntry.txt Prefix file found, named: exampleTxt.txt Suffix file found, named: exampleFileEntry.txt Suffix file found, named: exampleTxt.txt Or file found, named: example Or file found, named: exampleFileEntry.txt Or file found, named: exampleTxt.txt And/Not file found, named: example 1.4比较器 org.apache.commons.io.comparator软件包包含一些类这些类使我们可以轻松地对文件和目录进行比较和排序。 我们只需要提供文件列表并根据类以各种方式对它们进行比较。 ComparatorExample.java import java.io.File; import java.util.Date;import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; import org.apache.commons.io.comparator.LastModifiedFileComparator; import org.apache.commons.io.comparator.NameFileComparator; import org.apache.commons.io.comparator.SizeFileComparator;public final class ComparatorExample {private static final String PARENT_DIR C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder;private static final String FILE_1 C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\example;private static final String FILE_2 C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleTxt.txt;public static void runExample() {System.out.println(Comparator example...);//NameFileComparator// Lets get a directory as a File object// and sort all its files.File parentDir FileUtils.getFile(PARENT_DIR);NameFileComparator comparator new NameFileComparator(IOCase.SENSITIVE);File[] sortedFiles comparator.sort(parentDir.listFiles());System.out.println(Sorted by name files in parent directory: );for (File file: sortedFiles) {System.out.println(\t file.getAbsolutePath());}// SizeFileComparator// We can compare files based on their size.// The boolean in the constructor is about the directories.// true: directorys contents count to the size.// false: directory is considered zero size.SizeFileComparator sizeComparator new SizeFileComparator(true);File[] sizeFiles sizeComparator.sort(parentDir.listFiles());System.out.println(Sorted by size files in parent directory: );for (File file: sizeFiles) {System.out.println(\t file.getName() with size (kb): file.length());}// LastModifiedFileComparator// We can use this class to find which file was more recently modified.LastModifiedFileComparator lastModified new LastModifiedFileComparator();File[] lastModifiedFiles lastModified.sort(parentDir.listFiles());System.out.println(Sorted by last modified files in parent directory: );for (File file: lastModifiedFiles) {Date modified new Date(file.lastModified());System.out.println(\t file.getName() last modified on: modified);}// Or, we can also compare 2 specific files and find which one was last modified.// returns 0 if the first file was last modified.// returns 0)System.out.println(File file1.getName() was modified last because...);elseSystem.out.println(File file2.getName() was modified last because...);System.out.println(\t file1.getName() last modified on: new Date(file1.lastModified()));System.out.println(\t file2.getName() last modified on: new Date(file2.lastModified()));} } 输出量 Comparator example... Sorted by name files in parent directory: C:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\comparator1.txtC:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\comperator2.txtC:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\exampleC:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\exampleFileEntry.txtC:\Users\Lilykos\workspace\ApacheCommonsExample\ExampleFolder\exampleTxt.txt Sorted by size files in parent directory: example with size (kb): 0exampleTxt.txt with size (kb): 87exampleFileEntry.txt with size (kb): 503comperator2.txt with size (kb): 1458comparator1.txt with size (kb): 4436 Sorted by last modified files in parent directory: exampleTxt.txt last modified on: Sun Oct 26 14:02:22 EET 2014example last modified on: Sun Oct 26 23:42:55 EET 2014comparator1.txt last modified on: Tue Oct 28 14:48:28 EET 2014comperator2.txt last modified on: Tue Oct 28 14:48:52 EET 2014exampleFileEntry.txt last modified on: Tue Oct 28 14:53:50 EET 2014 File example was modified last because...example last modified on: Sun Oct 26 23:42:55 EET 2014exampleTxt.txt last modified on: Sun Oct 26 14:02:22 EET 2014 让我们看看这里使用了哪些类 NameFileComparator 根据文件名比较文件。 SizeFileComparator 根据文件大小比较文件。 LastModifiedFileComparator 根据文件的最后修改日期比较文件。 您还应该在这里注意比较可以在整个目录中进行使用sort()方法sort()它们进行排序也可以在两个文件中分别进行compare()使用compare() 。 1.5输入 org.apache.commons.io.input包中有InputStream各种实现。 我们将研究最有用的一个TeeInputStream 它同时使用InputStream和OutputStream作为参数并自动将从输入中读取的字节复制到输出中。 此外通过使用第三个布尔值参数最后只关闭TeeInputStream 两个附加流也将关闭。 InputExample.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException;import org.apache.commons.io.FileUtils; import org.apache.commons.io.input.TeeInputStream; import org.apache.commons.io.input.XmlStreamReader;public final class InputExample {private static final String XML_PATH C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\InputOutputExampleFolder\\web.xml;private static final String INPUT This should go to the output.;public static void runExample() {System.out.println(Input example...);XmlStreamReader xmlReader null;TeeInputStream tee null;try {// XmlStreamReader// We can read an xml file and get its encoding.File xml FileUtils.getFile(XML_PATH);xmlReader new XmlStreamReader(xml);System.out.println(XML encoding: xmlReader.getEncoding());// TeeInputStream// This very useful class copies an input stream to an output stream// and closes both using only one close() method (by defining the 3rd// constructor parameter as true).ByteArrayInputStream in new ByteArrayInputStream(INPUT.getBytes(US-ASCII));ByteArrayOutputStream out new ByteArrayOutputStream();tee new TeeInputStream(in, out, true);tee.read(new byte[INPUT.length()]);System.out.println(Output stream: out.toString()); } catch (IOException e) {e.printStackTrace();} finally {try { xmlReader.close(); }catch (IOException e) { e.printStackTrace(); }try { tee.close(); }catch (IOException e) { e.printStackTrace(); }}} } 输出量 Input example... XML encoding: UTF-8 Output stream: This should go to the output. 1.6输出 与org.apache.commons.io.input相似 org.apache.commons.io.output具有OutputStream实现可以在许多情况下使用。 一个非常有趣的是TeeOutputStream 它允许将输出流进行分支换句话说我们可以将输入流发送到2个不同的输出。 OutputExample.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException;import org.apache.commons.io.input.TeeInputStream; import org.apache.commons.io.output.TeeOutputStream;public final class OutputExample {private static final String INPUT This should go to the output.;public static void runExample() {System.out.println(Output example...);TeeInputStream teeIn null;TeeOutputStream teeOut null;try {// TeeOutputStreamByteArrayInputStream in new ByteArrayInputStream(INPUT.getBytes(US-ASCII));ByteArrayOutputStream out1 new ByteArrayOutputStream();ByteArrayOutputStream out2 new ByteArrayOutputStream();teeOut new TeeOutputStream(out1, out2);teeIn new TeeInputStream(in, teeOut, true);teeIn.read(new byte[INPUT.length()]);System.out.println(Output stream 1: out1.toString());System.out.println(Output stream 2: out2.toString());} catch (IOException e) {e.printStackTrace();} finally {// No need to close teeOut. When teeIn closes, it will also close its// Output stream (which is teeOut), which will in turn close the 2// branches (out1, out2).try { teeIn.close(); }catch (IOException e) { e.printStackTrace(); }}} } 输出量 Output example... Output stream 1: This should go to the output. Output stream 2: This should go to the output. 2.下载完整示例 这是Apache Commons IO的简介 涵盖了大多数为开发人员提供简单解决方案的重要类。 这个庞大的软件包中还有许多其他功能但是通过使用此介绍您可以了解将来的项目的总体思路和一些有用的工具 下载 您可以在此处下载此示例的完整源代码 ApacheCommonsIOExample.rar 翻译自: https://www.javacodegeeks.com/2014/10/apache-commons-io-tutorial.html
http://www.pierceye.com/news/716407/

相关文章:

  • 购买腾讯备案网站网站错误列表
  • 怎么查看网站的外链php网站建设流程图
  • 顺企网萍乡网站建设wordpress 读者墙
  • 电力建设期刊网站投稿域名提供商
  • 广东网站备案需要多久oa信息化管理系统平台
  • 哪个网站可以做担保交易小程序排行榜
  • 网站用html做的怎么弄后台中铁十六局个人门户网
  • 一个网站怎么做流量统计佛山市seo广告优化工具
  • 机关网站建设需求文档国家住建部官网
  • 一条龙网站建设哪家好六安招聘网官网
  • 网站建设 中企动力阀门和建设银行类似的网站
  • 所有做运动的网站姜堰网网站
  • 广西汽车网网站建设影楼微网站建设方案
  • 企业展厅设计比较好的公司北京优化服务
  • 网站的icp 备案信息wordpress爆破字典
  • 福建厦门网站建设公司网站代码素材建设
  • 广州网络公司建站e语言可以做网站吗
  • 不想用原来的网站模板了就用小偷工具采集了一个可是怎么替换seo顾问张智伟
  • 效果好的徐州网站开发建设网站怎么学
  • 上海网站设计要多少钱建设银行个人网站打不开
  • 哪个网站做欧洲旅行比较好东营网站制作
  • 做pc端网站效果wordpress js 添加图片
  • 给装修公司做网站商标设计大全
  • 深圳做网站公司有哪些地方国际形势最新消息
  • 企业网站建设管理平台石家庄平山网站推广优化
  • 免费asp网站模板带后台网站建设需求调研通知
  • 浙江二建建设集团有限公司网站微信哪里可以做视频网站
  • wordpress阿里百秀5.2广州网站排名专业乐云seo
  • 网站建设 上海网站福州最好的网站建设公司
  • 兴力网站建设企业宣传网站在哪里做