`
crabdave
  • 浏览: 1294813 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Listing all files from a directory with a giving suffix

    博客分类:
  • Java
阅读更多

Listing all files from a directory with a giving suffix

 

       最近办离职,需要列一下代码清单,网上找了段程序,保留一下。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FilesReader {

	private static final String READ_PATH = "D:/WebRoot/market";
//	private static final String SUFFIXAL_PATH = "java";
	private static final String SUFFIXAL_PATH = "jsp";
//	private static final String SUFFIXAL_PATH = "js";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List arrayList = FilesReader.getListFiles(READ_PATH, SUFFIXAL_PATH, true);

		if (arrayList.isEmpty()) {
			System.out.println("没有符号要求的文件");
		} else {
			String message = "";
			message += "符号要求的文件数:" + arrayList.size() + "\r\n";
			System.out.println(message);
//			String lastPath="";
			for (Iterator i = arrayList.iterator(); i.hasNext();) {
				String temp = (String) i.next();
				String path=temp.substring(0, temp.lastIndexOf("\\"));
				String name=temp.substring(temp.lastIndexOf("\\")+1,temp.length());
				//System.out.println(temp);
//				if(!lastPath.equals(path)){
//					lastPath=path;
//					System.out.println("\n");
//				}
				System.out.println(name+"\t\t\t\t\t"+path);
				message += temp + "\r\n";
			}

//			appendMethod("d:/ajax/menu.txt", message);
		}
	}

	public static List<String> fileList = new ArrayList<String>();

	/**
	 * 
	 * @param path
	 *            文件路径
	 * @param suffix
	 *            后缀名
	 * @param isdepth
	 *            是否遍历子目录
	 * @return
	 */
	public static List getListFiles(String path, String suffix, boolean isdepth) {
		File file = new File(path);
		return FilesReader.listFile(file, suffix, isdepth);
	}

	public static List listFile(File f, String suffix, boolean isdepth) {
		// 是目录,同时需要遍历子目录
		if (f.isDirectory() && isdepth == true) {
			File[] t = f.listFiles();
			for (int i = 0; i < t.length; i++) {
				listFile(t[i], suffix, isdepth);
			}
		} else {
			String filePath = f.getAbsolutePath();

			if (suffix != null) {
				int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引
				String tempsuffix = "";

				if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件
				{
					tempsuffix = filePath.substring(begIndex + 1, filePath
							.length());
				}

				if (tempsuffix.equals(suffix)) {
					fileList.add(filePath);
				}
			} else {
				// 后缀名为null则为所有文件
				fileList.add(filePath);
			}

		}

		return fileList;
	}

	/**
	 * 方法追加文件:使用FilesReader
	 * 
	 * @param fileName
	 * @param content
	 */
	public static void appendMethod(String fileName, String content) {
		try {
			// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(content + "\r\n");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

分享到:
评论

相关推荐

    filelisting

    Listing the files inside a directory http://www.geekpedia.com/tutorial148_Listing-the-files-inside-a-directory.html

    view-files-list-html.zip_directory_html_listing

    描述“Generate a files listing in html”进一步确认了这一点,它表明这个压缩包包含了一个可以将文件系统中的文件和目录转换为HTML格式的列表的软件。 在IT领域,这样的工具通常用于创建网站的索引页面,或者方便...

    PHP文件数据类网站实例开发源码——Simple Directory Listing 文件管理系统.zip

    Simple Directory Listing可能已经考虑到了这一点,使用了媒体查询(Media Queries)和响应式布局,确保在手机、平板电脑和桌面设备上都能提供良好的用户体验。 八、性能优化 对于大量文件的目录,优化文件遍历的...

    Linux Shell Scripting Cookbook

    Listing all the machines alive on a network 243 Transferring files 247 Setting up an Ethernet and wireless LAN with script 250 Password-less auto-login with SSH 253 Running commands on remote host ...

    Midnight Commander Win32 port

    cpio, lha and rar files with a single click. o All of the Midnight Commander operations work with the virtual file system, enabling you to do complex tasks. o Mouse support on the Linux console...

    Java Game Development with LibGDX: From Beginner to Professional.pdf

    Working through the examples in this book, you will create 12 game prototypes in a variety of popular genres, from collection-based and shoot-em-up arcade games to side-scrolling platformers and ...

    文件数据Simple Directory Listing v2.1 beta1 文件管理系统-sdl2.rar

    【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。...【项目质量】:所有源码都经过严格测试,可以直接...

    korweblog_directory_listing.nasl

    korweblog_directory_listing

    解决多多返利系统后台Directory Listing Denied

    多多返利系统后台无法打开,出现Directory Listing Denied This Virtual Directory does not allow contents to be listed. 解决办法文件!保证可用!

    ssd5 ex4 给需要帮助的人。这是ssd5 ex4

    This method a new instance of a Listing object that contains only those advertisements whose name or description contains the string given by the parameter keyword. If the parameter keyword equals ...

    TCL TK 语言8.5编程指导

    Returning a range of characters from a string 59 Creating a string of repeated characters 60 Replacing ranges of characters contained within a string 60 Creating a reverse string 61 Converting a ...

    PHP Directory Listing-开源

    【PHP Directory Listing 开源解决方案详解】 在Web开发中,服务器默认的目录列表通常只提供基本的文件和目录信息,对于用户来说,这种显示方式可能不够友好。PHP Directory Listing开源项目正是为了解决这一问题,...

    Code-Listing-8.1---Text-Files.zip_Programming with C

    标题中的"Code-Listing-8.1---Text-Files.zip_Programming with C"表明这是一个与C语言编程相关的代码示例集合,特别是涉及到文本文件处理的部分。在C编程中,处理文本文件是至关重要的,这通常包括读取、写入、追加...

    VC技术内幕第五版.chm

    You can copy the contents of the CD to your hard disk by running the setup program found in the CD's root directory, or you can retrieve the files directly from the CD's \Code directory. The \Code ...

    Sudoku Programming With C

    And, from the very start, he decided that he would publish the code for anyone else to use and perhaps tinker with, but the author wrote it in such a way that also lets readers with limited knowledge...

    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. - **...

    Python核心编程第三版(源代码)

    Python核心编程第三版 各个章节的源代码 与内容 README "Core Python Programming", 2nd Edition...fully-recursive directory listing. The "all.zip" and "all.tgz" files represent snapshots of everything here.

    2009 达内Unix学习笔记

    -a 所有(all)。 -e 所有(every),比a更详细。 -f 取消保护。 -i 添加提示。 -p 强制执行。 -r 目录管理。 分屏显示的中途操作 空格&lt;space&gt; 继续打开下一屏; 回车&lt;return&gt; 继续打开下一行; b 另外开上...

    计算机网络第六版答案

    On the downstream channel, all packets emanate from a single source, namely, the head end. Thus, there are no collisions in the downstream channel. In most American cities, the current ...

Global site tag (gtag.js) - Google Analytics