在java中, 对一个磁盘里的所有文件进行遍历 ,把所有文件存放到一个缓冲区中,请问该如何实现???/谢谢 比如说C:

解决方案 »

  1.   

    这个是d读取c:下面的所有的txt文件。 File f=new File("c:\\");
    String[] names=f.list(new FilenameFilter(){
    public boolean accept(File dir,String name){
    return name.indexOf(".txt")!=-1;//过滤掉不是txt的文件
    }
    });
    for(int i=0;i<names.length;i++){
        File read=new File("c:\\"+names[i]);
    if(read.canRead()){
    BufferedInputStream in =new BufferedInputStream(new FileInputStream("c:\\"+names[i]));
    Long size=read.length();
    byte[] buf=new byte[size.intValue()];//
    int len=in.read(buf);
    System.out.println(new String(buf,0,len));
    }

    }
      

  2.   

    格式重新整理了一哈···
    File f=new File("c:\\");
    String[] names=f.list(new FilenameFilter(){
            public boolean accept(File dir,String name){
                return name.indexOf(".txt")!=-1;//过滤掉不是txt的文件
            }
         });
        for(int i=0;i<names.length;i++){
             File read=new File("c:\\"+names[i]);
    if(read.canRead()){
                BufferedInputStream in =new BufferedInputStream(new FileInputStream("c:\\"+names[i]));
       Long size=read.length();//读取文件的大小
       byte[] buf=new byte[size.intValue()];
       int len=in.read(buf);
       System.out.println(new String(buf,0,len));
    }

         }
      

  3.   

    如果要要读取C:下面的所有文件···包括子目录下面的文件··就不需要添加FilenameFilter过滤器然后在读取的时候需要添加一个isDirectory()来判断是否为目录,如果是则读取该目录下面的文件,依此类推可以用递归来做···
    还要注意的是:还要判断文件是否可读canread()
      

  4.   

    bygones2001(没辙了)  所以我没写全部撒··
    只是给一个思路···
    而且这代码是以前写过的,不存在“埋头写代码抢分”
      

  5.   

    引用bygones2001(没辙了) 的意思,无可行性 ····缓冲区没那么大 ···