Click here to Skip to main content
15,881,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
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
Posted
Updated 9-Mar-18 22:56pm
Comments
Jochen Arndt 9-Mar-18 8:01am    
Where is your OnlyExt implementation?

This looks very similar to FilenameFilter - sample program in Java[^], which you should check for correct copying.
 
Share this answer
 
Comments
four systems 10-Mar-18 3:33am    
error is the same again
Richard MacCutchan 10-Mar-18 3:59am    
Very useful problem analysis - not.
four systems 10-Mar-18 3:36am    
probably something to do with file path which changed several times
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);
		}
	}


prints JsCounts.html
1
JsForm.html
2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900