F:\linhaiwen\j2ee\java>javac ReadMultiFile.java
ReadMultiFile.java:9: non-static variable this cannot be referenced from a stati
c context
        FileFilter fFilter=new FileFilter("f:\\linhaiwen\\j2ee","java");
                           ^
1 error

解决方案 »

  1.   

    FileFilter是interface,不能直接new的
      

  2.   

    嵌套类放到外面来放到外面后
    import java.io.*;
    import java.util.*;
    class ReadMutilFile 
    {
    public static void main(String[] args) throws Exception
    {
    try
    {
    FileFilter fFilter=new FileFilter("f:\\linhaiwen\\j2ee","java");
    SequenceInputStream seqInput=new SequenceInputStream(fFilter);
    FileOutputStream fOutput=new FileOutputStream("sourcesum.java");
    int nContent;
    while ((nContent=seqInput.read())!=-1)
    {
    fOutput.write(nContent);
    }
    seqInput.close();
    fOutput.close();
    }
    catch(FileNotFoundException e1)
    {
    e1.printStackTrace();
    }
    // System.out.println("Hello World!");
    }
    }
    class FileFilter implements Enumeration
    {
    File directory;
    String extension;
    String strFileList[];
    Filter filter;
    int nCounter;
    FileFilter(String strDirectoryName,String extension)
    {
    this.extension=extension;
    directory=new File(strDirectoryName);
    if (directory.isDirectory())
    {
    filter=new Filter(extension);
    strFileList=directory.list(filter);
    }
    else 
    {
    strFileList=new String[1];
    strFileList[0]=strDirectoryName;
    }
    nCounter=0;
    }
    public boolean hasMoreElements()
    {
    if (nCounter<strFileList.length)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    public Object nextElement()
    {
    // try
    // {
    if (hasMoreElements())
    {
    nCounter++;
    return new FileInputStream(strFileList[nCounter-1]);
    }
    else 
    return null;
    // }
    // catch(FileNotFoundException e2)
    // {
    // e2.printStackTrace();
    // }
    }
    }
    class Filter implements FilenameFilter
    {
    String extension;
    Filter(String extension)
    {
    this.extension=extension;
    }
    public boolean accept(File directory,String filename)
    {
    return filename.endsWith("."+extension);
    }
    }
    F:\linhaiwen\j2ee>javac ReadMultiFile.java
    ReadMultiFile.java:68: unreported exception java.io.FileNotFoundException; must
    be caught or declared to be thrown
                            return new FileInputStream(strFileList[nCounter-1]);
                                   ^
    1 error这里要怎么抛出异常,哪位可否编译通过后贴上来,我马上结贴
      

  3.   

    将 nextElement()方法替换成下面的,编译能通过,运行怎么样不知道
    public Object nextElement()
    {
    // try
    // {
    if (hasMoreElements())
    {
    nCounter++;
    FileInputStream fis=null;
    try{
    fis=new FileInputStream(strFileList[nCounter-1]);
    }catch(FileNotFoundException ex){
    System.out.println("找不到指定文件");
    ex.printStackTrace();
    }
    return fis;
    }
    else 
    return null;
    // }
    // catch(FileNotFoundException e2)
    // {
    // e2.printStackTrace();
    // }
    }
    }