Example usage for java.io File list

List of usage examples for java.io File list

Introduction

In this page you can find the example usage for java.io File list.

Prototype

public String[] list() 

Source Link

Document

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

Usage

From source file:DirList2.java

public static void main(String[] args) {
    File path = new File(".");
    String[] list;/* ww  w.j  a  v a  2  s. com*/
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(filter(args[0]));
    Arrays.sort(list, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:DirList3.java

public static void main(final String[] args) {
    File path = new File(".");
    String[] list;/*from www. ja v a  2  s . c o  m*/
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(args[0]);

            public boolean accept(File dir, String name) {
                return pattern.matcher(new File(name).getName()).matches();
            }
        });
    Arrays.sort(list, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:DirList.java

public static void main(String[] args) {
    File path = new File(".");
    String[] list;/*from  w w w . ja va 2  s  .  co m*/
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(new DirFilter(args[0]));
    Arrays.sort(list, new AlphabeticComparator());
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:DirList.java

public static void main(String args[]) {
    String dirname = "/java";
    File f1 = new File(dirname);

    if (f1.isDirectory()) {
        System.out.println("Directory of " + dirname);
        String s[] = f1.list();

        for (int i = 0; i < s.length; i++) {
            File f = new File(dirname + "/" + s[i]);
            if (f.isDirectory()) {
                System.out.println(s[i] + " is a directory");
            } else {
                System.out.println(s[i] + " is a file");
            }/* w w  w.ja va2s . c o  m*/
        }
    } else {
        System.out.println(dirname + " is not a directory");
    }
}

From source file:Empty.java

public static void main(String[] argv) {
    if (argv.length != 1) { // no progname in argv[0]
        System.err.println("usage: Empty dirname");
        System.exit(1);/*from  w ww .java 2  s. c o m*/
    }

    File dir = new File(argv[0]);
    if (!dir.exists()) {
        System.out.println(argv[0] + " does not exist");
        return;
    }

    String[] info = dir.list();
    for (int i = 0; i < info.length; i++) {
        File n = new File(argv[0] + dir.separator + info[i]);
        if (!n.isFile()) // skip ., .., other directories too
            continue;
        System.out.println("removing " + n.getPath());
        if (!n.delete())
            System.err.println("Couldn't remove " + n.getPath());
    }
}

From source file:Main.java

public static void main(String[] args) {
    File games = new File("C:\\Test");
    File[] files = games.listFiles();
    for (File file : files) {
        System.out.println(file + " is a " + (file.isDirectory() ? "directory" : "file"));
    }/*w  w  w. j  av a 2 s  .  c o m*/
    String[] xfiles = games.list();
    for (String file : xfiles) {
        System.out.println("File = " + file);
    }
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            if (name.endsWith(".txt")) {
                return true;
            }
            return false;
        }
    };

    File[] yfiles = games.listFiles(filter);
    for (File doc : yfiles) {
        System.out.println("Doc file = " + doc);
    }
}

From source file:gda.util.PackageMaker.java

/**
 * @param args//from  w w  w.  j a va  2 s . c om
 */
public static void main(String args[]) {
    String destDir = null;
    if (args.length == 0)
        return;
    String filename = args[0];
    if (args.length > 1)
        destDir = args[1];
    try {
        ClassParser cp = new ClassParser(filename);
        JavaClass classfile = cp.parse();
        String packageName = classfile.getPackageName();
        String apackageName = packageName.replace(".", File.separator);
        if (destDir != null)
            destDir = destDir + File.separator + apackageName;
        else
            destDir = apackageName;
        File destFile = new File(destDir);
        File existFile = new File(filename);
        File parentDir = new File(existFile.getAbsolutePath().substring(0,
                existFile.getAbsolutePath().lastIndexOf(File.separator)));
        String allFiles[] = parentDir.list();
        Vector<String> selectedFiles = new Vector<String>();
        String toMatch = existFile.getName().substring(0, existFile.getName().lastIndexOf("."));
        for (int i = 0; i < allFiles.length; i++) {
            if (allFiles[i].startsWith(toMatch + "$"))
                selectedFiles.add(allFiles[i]);
        }
        FileUtils.copyFileToDirectory(existFile, destFile);
        Object[] filestoCopy = selectedFiles.toArray();
        for (int i = 0; i < filestoCopy.length; i++) {
            FileUtils.copyFileToDirectory(new File((String) filestoCopy[i]), destFile);
        }
    }

    catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:KillFilesByName.java

public static void main(String[] argv) {
    if (argv.length != 2) {
        System.err.println("usage: KillFilesByName dirname pattern");
        System.exit(1);/*from  www.j  a  v a  2  s .  c  om*/
    }

    File dir = new File(argv[0]);
    if (!dir.exists()) {
        System.out.println(argv[0] + " does not exist");
        return;
    }
    String patt = argv[1];

    String[] info = dir.list();
    for (int i = 0; i < info.length; i++) {
        File n = new File(argv[0] + dir.separator + info[i]);
        if (!n.isFile()) { // skip ., .., other directories, etc.
            continue;
        }
        if (info[i].indexOf(patt) == -1) { // name doesn't match
            continue;
        }
        System.out.println("removing " + n.getPath());
        if (!n.delete())
            System.err.println("Couldn't remove " + n.getPath());
    }
}

From source file:com.sany.application.action.AppcreateAction.java

public static void main(String[] args0) {
    File file = new File("D:\\apache-maven-3.0.4\\bin");
    String test[];//from w  w w  .j ava  2  s . c o  m
    test = file.list();
    for (int i = 0; i < test.length; i++) {
        System.out.println(test[i]);
    }
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File(args[0]);
    if (!file.exists()) {
        System.out.println(args[0] + " does not exist.");
        return;/*from w  w  w  .  ja v  a 2s .  com*/
    }
    if (file.isFile() && file.canRead()) {
        System.out.println(file.getName() + " can be read from.");
    }
    if (file.isDirectory()) {
        System.out.println(file.getPath() + " is a directory containing...");
        String[] files = file.list();
        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i]);
        }
    }
}