列出HTML文件的Java代码 [英] Java code that lists HTML files

查看:89
本文介绍了列出HTML文件的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在位置列出html文件的代码



code that lists html files at location

import java.io.*;

public class DirectoryHtmlFiles {
    
}

// Directory of .HTML files. 

class DirListOnly { 
  public static void main(String args[]) { 
    String dirname = "C:\\Users\\Admin\\Documents\\NetBeansProjects\\JavaWeb\\web"; 
    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]); 
    } 
  } 





然而它出错了





however it gives the error

Quote:

线程main中的异常java.lang.RuntimeException:无法编译的源代码 - 错误的树类型:Io.File

在Io.DirListOnly.main(DirectoryHtmlFiles.java:14)

C:\ Users \Admin \ AppData \ Local \ NetBeans \Cache \ 8.2 \ executor-snippets\run.xml:53:Java返回:1

BUILD FAILED(总时间:0秒)

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




我尝试过:



用不同的组合更改了代码



What I have tried:

changed code with different combinations

推荐答案

这看起来非常类似于 FilenameFilter - Java中的示例程序 [ ^ ],其中你应该检查c正确复制。
This looks very similar to FilenameFilter - sample program in Java[^], which you should check for correct copying.


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



prints JsCounts.html
1
JsForm.html
2


这篇关于列出HTML文件的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆