显示文件类型的java代码

时间:2018-03-10 08:56:43

标签: java

在dir中显示文件类型的代码会给出错误

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: Io.File
    at Io.DirectoryHtmlFiles.main(DirectoryHtmlFiles.java:9)
C:\Users\Admin\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)"   `

有代码:代码用于包Io

package Io;
import java.io.*;

class DirectoryHtmlFiles { 
    public static void main(String args[]) { 
        String dirname = "D:\\"; 
        File f1 = new File(dirname); 
        FilenameFilter only = new OnlyExt("html"); 
        String s[] = f1.list(only); 
        for (int i=0; i < s.length; i++) { 
            System.out.println(s[i]); 
        } 
    } 
}

1 个答案:

答案 0 :(得分:0)

package javas;

import java.io.File;
import java.io.FilenameFilter;

public class DirectoryHtmlFiles {

public static void main(String a[]){
            int Count = 0;
    File file = new File("C:\\Users\\Admin\\Documents\\NetBeansProjects\\JavaWeb");
    String[] files = file.list(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            if(name.toLowerCase().endsWith(".html")){
                return true;
            } else {
                return false;
            }
        }
    });
    for(String f:files){
                    Count++;
        System.out.println(f);
                    System.out.println(Count);
    }
}

认为上面代码产生结果的另一种方式 JsCounts.html 1 JsForm.html 2 这是带有计数的文件类型,谢谢