`
morgan-java
  • 浏览: 56384 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Displays directory listing using regular expressions

阅读更多
// : c12:DirList.java
// Displays directory listing using regular expressions.
// {Args: "D.*\.java"}
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class DirList {
  public static void main(String[] args) {
    File path = new File(".");
    String[] list;
    if (args.length == 0)
      list = path.list();
    else
      list = path.list(new DirFilter(args[0]));
    Arrays.sort(list, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
      System.out.println(list[i]);
  }
}

class DirFilter implements FilenameFilter {
  private Pattern pattern;

  public DirFilter(String regex) {
    pattern = Pattern.compile(regex);
  }

  public boolean accept(File dir, String name) {
    // Strip path information, search for regex:
    return pattern.matcher(new File(name).getName()).matches();
  }
} ///:~

class AlphabeticComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    String s1 = (String) o1;
    String s2 = (String) o2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
} ///:~
分享到:
评论

相关推荐

    nanoFramework.System.Text.RegularExpressions:nanoFramework System.Text.RegularExpressions类库

    欢迎使用nanoFramework System.Text.RegularExpressions存储库!建置状态成分建置状态NuGet软件包System.Text.RegularExpressions System.Text.RegularExpressions(预览) 重要提示:此正则表达式解析器将满足您的...

    Moveable Interactive Projected Displays Using Projector Based Tracking

    ### 移动交互式投影显示使用基于投影仪的跟踪技术 #### 摘要与背景 本文介绍了一种创新的技术,即使用基于投影仪的位置跟踪技术来实现移动表面的交互式投影显示。传统上,视频投影仪主要用于在相对投影仪几何关系...

    Using LEDs, LCDs and GLCDs in Microcontroller Projects

    Describing the use of displays in microcontroller based projects, the author makes extensive use of real-world, tested projects. The complete details of each project are given, including the full ...

    Neon Glow Displays Vol_2_9.5.rar

    标题"Neon Glow Displays Vol_2_9.5.rar"表明这可能是一个关于Unity3D霓虹灯效果的资源包,版本号为9.5,可能包含升级或改进的霓虹灯显示效果。".rar"文件格式是常见的压缩文件格式,用于存储多个文件在一个包里。 ...

    Core Java(Volume II--Advanced Features 9th Edition)

    Streams, files, and regular expressions XML Networking Database programming facilities JNDI/LDAP directory integration Internationalization Advanced Swing techniques JavaBeans components Web services ...

    Neon Glow Displays Vol_2 v1.0.unitypackage

    一款运行于Unity平台的插件 霓虹灯光展示效果资源包Neon Glow Displays Vol_2 1.0 所支持的Unity版本:5.3.4 及以上版本 Amazing Neon display pack! This is a great pack include materials, prefabs, FX sc ripts ...

    vb 2008 抽奖程序 word文档

    i = System.Text.RegularExpressions.Regex.Matches(TextBox1.Text, "\r\n").Count '用一个数组来接收textbox1各行的信息 Shuzu = New String(i - 1) {} For x As Integer = 0 To i - 1 Shuzu(x) = TextBox1....

    新版完整标准 IEC 62906-5-5-2022 Laser displays - Part 5-5_ Optical

    新版完整标准 IEC 62906-5-5-2022 Laser displays - Part 5-5_ Optical measuring methods of raster-scanning retina direct projection laser displa.pdf

    UE(官方下载)

    An introduction to using Perl-style regular expressions for search/replace Perl regex tutorial: non-greedy regular expressions Have you ever built a complex Perl-style regular expression, only to find...

    Filtering Edges for Gray-Scale Displays

    ### 计算机图形学中的边缘过滤技术 #### 引言 在计算机图形学领域,尤其是在处理基于栅格(像素网格)的图像时,如何绘制平滑的线条和边缘一直是一个重要的研究课题。传统的简单线描算法往往会产生“锯齿状”(即...

    openGL.rar_lines

    a program which reads XYL information defining points and lines in 2D and displays an image using OpenGL

    EmEditor中文版

    Right clicking on each marker in the Markers toolbar displays a context menu, and allows you to enable or disable each marker, search for each marker, or change the match condition (case sensitivity, ...

    A Delphi Developers Guide for 4K Displays

    With modern Ultra-HD displays ("4K displays") at sizes of 28" or smaller, this game has changed forever. Simply because no human being is sharp-sighted enough to comfortably read text at the size of a...

    Neon Glow Displays Vol_2 v1.0.rar

    《Unity中的霓虹光效展示:Neon Glow Displays Vol_2 v1.0》 Unity,作为一款全球领先的实时3D内容创作和运营平台,深受游戏开发者和视觉艺术家的青睐。其中,各种特效的实现是提升游戏视觉效果、增强用户体验的...

    Unix shell programming in 24 hours.pdf

    - **Listing Files** The `ls` command is used to list the files and directories in a directory. Options like `-l` for detailed listings and `-a` for showing hidden files enhance its functionality. - **...

    squashfs1.3r3.tar.gz

    regular file, directory, symbolic link, and block/char device inodes have different sizes). 5. Squashfs can use block sizes up to 32K (the default size is 32K). Using 32K blocks achieves greater ...

    senors and displays based on structural colour

    senors and displays based on structural colour; qualcomm P-INK, Morpho butterfly wing scales demonstrate highly selective vapour response

    Allen Bradley PLC Programming using Factory IO Factory IO 编程

    Allen Bradley PLC Programming using Factory IO - Emitters, Digital Displays, Integers Allen-Bradley PLC使用Factory IO 编程-发射器、数字显示器、积分器

    Screen Saver which Displays Pictures一个简单的屏幕保护程序(5KB)

    "Screen Saver which Displays Pictures" 是这样一个简单的屏幕保护程序,它会在闲置时启动,并展示图片来活动屏幕。 这个5KB的小程序虽然体积小巧,但其背后涉及的技术原理却很丰富。首先,屏幕保护程序的运行依赖...

Global site tag (gtag.js) - Google Analytics