Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(String[] args) {

        File f = new File("c:/test");

        FilenameFilter filter = new MainFilenameFilter("txt");

        // array of files and directory
        String[] paths = f.list(filter);

        // for each name in the path array
        for (String path : paths) {
            System.out.println(path);
        }

    }
}

class MainFilenameFilter implements FilenameFilter {

    String str;

    // constructor takes string argument
    public MainFilenameFilter(String ext) {
        str = "." + ext;
    }

    @Override
    public boolean accept(File dir, String name) {
        return name.endsWith(str);
    }
}