Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

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

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:Main.java

public static void main(String[] args) {
    // Change the dirPath value to list files from your directory
    String dirPath = "C:\\";

    File dir = new File(dirPath);
    File[] list = dir.listFiles();

    for (File f : list) {
        if (f.isFile()) {
            System.out.println(f.getPath() + "  (File)");
        } else if (f.isDirectory()) {
            System.out.println(f.getPath() + "  (Directory)");
        }/*from w w w .j a  va  2 s.  c o  m*/
    }
}

From source file:Main.java

public static void main(String[] a) {
    File myFile = new File("C:" + File.separator);
    for (File s : myFile.listFiles()) {
        System.out.println(s);/*  w w w  . jav a 2s  . c o m*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    File dir = new File("c:\\");
    File[] files = dir.listFiles();

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
    for (int i = 0; i < files.length; i++) {
        File file = files[i];//from   w w w.  jav a  2  s .co m
        System.out.printf("File %s - %2$tm %2$te,%2$tY%n= ", file.getName(), file.lastModified());
    }

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        System.out.printf("File %s - %2$tm %2$te,%2$tY%n= ", file.getName(), file.lastModified());
    }
}

From source file:Main.java

public static void main(String[] args) {
    final String dir = "E:\\Java_Dev\\";
    final File file = Paths.get(dir).toFile();
    File[] files = file.listFiles();
    Arrays.sort(files, (f1, f2) -> {
        if (f1.isDirectory() != f2.isDirectory()) {
            if (f1.isDirectory()) {
                return -1;
            } else {
                return 1;
            }/* ww  w  .ja  v a2s .co m*/
        } else {
            return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
        }
    });
    Stream.of(files).forEach(System.out::println);
}

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"));
    }//from w w  w .  j ava 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:Main.java

public static void main(String[] args) throws IOException {
    String path = "C:/Pictures/";
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();
    DefaultListModel listModel = new DefaultListModel();
    int count = 0;
    for (int i = 0; i < listOfFiles.length; i++) {
        String name = listOfFiles[i].toString();
        if (name.endsWith("jpg")) {
            ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
            listModel.add(count++, ii);//from  w w w. java 2 s. com
        }
    }
    JList lsm = new JList(listModel);
    lsm.setVisibleRowCount(1);

    frame.add(new JScrollPane(lsm));

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    // create new file
    File f = new File("c:/test");

    // returns pathnames for files and directory
    File[] paths = f.listFiles();

    // for each pathname in pathname array
    for (File path : paths) {
        System.out.println(path);
    }//from   www . j  a  va 2  s.c o m

}

From source file:com.cardFetcher.Core.NetrunnerCardFetcher.java

public static void main(String[] args) {
    //        String setsPath = "GameDatabase\\0f38e453-26df-4c04-9d67-6d43de939c77\\Sets";
    //        String imagesPath = "ImageDatabase\\0f38e453-26df-4c04-9d67-6d43de939c77\\Sets";
    String setsPath = "C:\\Users\\jose.gonzalez\\Documents\\OCTGN\\GameDatabase\\0f38e453-26df-4c04-9d67-6d43de939c77\\Sets";
    String imagesPath = "C:\\Users\\jose.gonzalez\\Documents\\OCTGN\\ImageDatabase\\0f38e453-26df-4c04-9d67-6d43de939c77\\Sets";
    //        String setsPath = "C:\\Users\\jgonzal2\\Documents\\OCTGN\\GameDatabase\\0f38e453-26df-4c04-9d67-6d43de939c77\\Sets";
    //        String imagesPath = "C:\\Users\\jgonzal2\\Documents\\OCTGN\\ImageDatabase\\0f38e453-26df-4c04-9d67-6d43de939c77\\Sets";

    try {/*from  w w  w. j ava2 s.c  o  m*/
        JSONArray json = JSONFetcher.getJSON();
        //System.out.println(json);
        File f = new File(setsPath);
        File[] files = f.listFiles();
        int count = 0;
        for (File file : files) {
            if (file.isDirectory()) {
                Set s = XmlParser.parseXML(file.getCanonicalPath() + "\\set.xml");

                if ((!s.getSetName().equals("Promos")) && (!s.getSetName().equals("Markers"))) {
                    //System.out.println(s);
                    ImageFetcher fetch = new ImageFetcher(s, imagesPath, json);
                    System.out.print("thread " + count++ + ": ");
                    fetch.start();
                }
            }
        }
    }

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

From source file:ddf.metrics.reporting.internal.rrd4j.SampleDataGenerator.java

public static void main(String[] args) {
    if (args.length == 1) {
        try {//from  ww  w . j av  a2 s . c  om
            String installLoc = args[0];
            File metricsDir = new File(installLoc, "/data/metrics");
            File[] files = metricsDir.listFiles();
            if (files != null) {
                for (File metricsFile : files) {
                    String metricsFileName = metricsFile.getName();
                    if (!metricsFileName.endsWith(".rrd")) {
                        continue;
                    }
                    RrdDb oldDb = new RrdDb(metricsFile.getAbsolutePath());
                    if (oldDb.getDsCount() > 1) {
                        continue;
                    }
                    DsType dsType = oldDb.getDatasource(0).getType();
                    String newDb = "target/" + metricsFileName;
                    long startTime = new DateTime().minusYears(1).getMillis();
                    int sampleSize = (int) ((new DateTime().getMillis() - startTime) / (60 * 1000));
                    new RrdMetricsRetrieverTest.RrdFileBuilder().rrdFileName(newDb).dsType(dsType)
                            .numSamples(sampleSize).numRows(sampleSize).startTime(startTime).build();
                    FileUtils.copyFile(new File(newDb), metricsFile);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    exit(0);
}

From source file:Snippet8.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Lazy Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        TreeItem root = new TreeItem(tree, 0);
        root.setText(roots[i].toString());
        root.setData(roots[i]);/*ww  w. j av  a 2  s .c om*/
        new TreeItem(root, 0);
    }
    tree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(final Event event) {
            final TreeItem root = (TreeItem) event.item;
            TreeItem[] items = root.getItems();
            for (int i = 0; i < items.length; i++) {
                if (items[i].getData() != null)
                    return;
                items[i].dispose();
            }
            File file = (File) root.getData();
            File[] files = file.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                TreeItem item = new TreeItem(root, 0);
                item.setText(files[i].getName());
                item.setData(files[i]);
                if (files[i].isDirectory()) {
                    new TreeItem(item, 0);
                }
            }
        }
    });
    Point size = tree.computeSize(300, SWT.DEFAULT);
    int width = Math.max(300, size.x);
    int height = Math.max(300, size.y);
    shell.setSize(shell.computeSize(width, height));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}