当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Listing all files from a directory with a giving suffix

发表于: 2011-05-31   作者:crabdave   来源:转载   浏览次数:
摘要: Listing all files from a directory with a giving suffix          最近办离职,需要列一下代码清单,网上找了段程序,保留一下。 import java.io.File; import java.io.FileWriter; import 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();
		}
	}
}

 

Listing all files from a directory with a giving suffix

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
http://blog.falchionconsulting.com/index.php/2012/03/stamping-pdf-files-downloaded-from-share
Many applications demand dynamic generation of PDF documents. Such applications range from ba
中午无聊,就随便弄了个桌面 其实简单地要老命,但是效果还是很好的哦.Google的配色方案给图片带来了
How to select n numbers from 1 to m with all of array 'all' and none of array 'none'? Try the
How to select n numbers from 1 to m with all of array 'all' and none of array 'none'? Try the
在搭建好Hadoop Eclipse开发环境后,编写map-reduce,遇到如下的问题: 从字面上可以看出,工程缺少
The following is an attempt to describe the Ukkonen algorithm by first showing what it does w
The following is an attempt to describe the Ukkonen algorithm by first showing what it does w
后缀数组(suffix array)详解 转载请注明:http://www.cnblogs.com/crazyacking/p/3988683.html 一.W
Suffix Trie 又称后缀Trie或后缀树。它与Trie树的最大不同在于,后缀Trie的字符串集合是由指定字符
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号