黑马程序员技术交流社区

标题: 请各位大哥列出一个File类中listFiles(FileFilter filter)的程序。 [打印本页]

作者: 何仕映    时间: 2013-5-11 19:40
标题: 请各位大哥列出一个File类中listFiles(FileFilter filter)的程序。
本帖最后由 何仕映 于 2013-5-12 21:33 编辑

listFiles(FileFilter filter)
         在FileFilter接口中accept方法的说明是测试指定抽象路径名是否应该包含在某个路径名列表中。我不大能理解是什么意思。请各位大哥,写个例子帮助我理解一下。谢谢
作者: jingzhouheima    时间: 2013-5-12 00:06

楼主看看下面的代码。。共同交流学习
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileStreamTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "";
            string content = "";
            Console.WriteLine("请输入要保存的文件名,包括路径");
            path = Console.ReadLine();
            Console.WriteLine("请输入要保存的内容");
            content = Console.ReadLine();
            try
            {
                FileStream file = new FileStream(path, FileMode.Create);
                byte[] bt = Encoding.UTF8.GetBytes(content);
                file.Write(bt, 0, bt.Length);
                //file.Flush();
            }
            catch (Exception ce)
            {
                Console.WriteLine("创建或写入文件时出错");
            }

        }
    }
}
//读取文件
/*namespace FileStreamTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "";
            Console.WriteLine("请输入要读取文件的文件名,包括路径");
            path = Console.ReadLine();
            if(!File.Exists(path))
            {
                Console.WriteLine("文件不存在");
                return;
            }
            try
            {
                FileStream file = new FileStream(path, FileMode.Open);
                byte[] bt = new byte[file.Length];
                file.Read(bt, 0, bt.Length);
                string str = Encoding.Default.GetString(bt);
                Console.WriteLine(str);
                Console.ReadLine();
            }
            catch(Exception ce)
            {
                Console.WriteLine("读取文件出错");
            }
        }
    }
}*/

   /* //写入
            string path = "d:/aa.txt";
            string str2 = "abcde";
            char[] str=new char[5];
            str=str2.ToCharArray();
            FileStream fs=new FileStream(path,FileMode.OpenOrCreate);
            fs.Seek(0, SeekOrigin.End);
            byte[] bt = Encoding.UTF8.GetBytes(str);
            fs.Write(bt, 0, bt.Length);
            //读取
            string path = "d:/aa.txt";
            FileStream fs=new FileStream(path,FileMode.OpenOrCreate);
            byte[] bt =new byte[100];
            fs.Read(bt, 0, bt.Length);
            string str = Encoding.Default.GetString(bt);
            Console.WriteLine(str);*/
作者: 刘胜寒    时间: 2013-5-12 10:05
FileFilter是一个文件过滤器。
一般采用匿名类来用listFileter 。
过滤出后缀名为“.xxx”的文件
  1. public static void main(String[] args)throws IOException
  2. {       
  3.           File dir = new File("C:\\");
  4.           File[] arr = dir.listFiles(new FilenameFilter()
  5.           {
  6.                   public boolean accept(File dir,String name)
  7.                   {
  8.                           return name.endsWith(".bin");
  9.                   }
  10.           }  );
  11.           for(File name:arr)
  12.           {
  13.                   System.out.println(name.toString());
  14.           }
  15. }
复制代码
你可以自己手写一个 调用 ListFiles的无参函数,获取文件列表然后根据自己的需求写一个。

listFiles(FileFilter filter)中的accept就是你要的需求。

如果明白就结贴,谢谢合作。。不明天继续提问
作者: 何仕映    时间: 2013-5-12 13:07
刘胜寒 发表于 2013-5-12 10:05
FileFilter是一个文件过滤器。
一般采用匿名类来用listFileter 。
过滤出后缀名为“.xxx”的文件你可以自己 ...

我主要是对FileFilter这个接口不明白,麻烦您再写一个实现这个接口的小程序,好吗?万分感谢。
作者: kimi    时间: 2013-5-12 15:16
  1. package com.kimi.demo;
  2. import java.io.File;
  3. public class TestDemo {
  4.         public static void main(String[] args) throws Exception {
  5.                 File file = new File("d:" + File.separator); // 给定一个操作路径
  6.                 print(file);
  7.         }
  8.         public static void print(File file) {
  9.                 if (file.exists() && file.isDirectory()) {
  10.                         File result[] = file.listFiles(); // 列出全部的目录内容
  11.                         if (result != null) {
  12.                                 for (int x = 0; x < result.length; x++) {
  13.                                         print(result[x]);
  14.                                 }
  15.                         }
  16.                 } else {
  17.                         System.out.println(file);
  18.                 }
  19.         }
  20. }
复制代码

作者: 刘胜寒    时间: 2013-5-12 18:00
何仕映 发表于 2013-5-12 13:07
我主要是对FileFilter这个接口不明白,麻烦您再写一个实现这个接口的小程序,好吗?万分感谢。 ...

写一个就够了吧。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2