Example usage for java.io File isDirectory

List of usage examples for java.io File isDirectory

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Tests whether the file denoted by this abstract pathname is a directory.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File dir = new File("c:\\temp");
    FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
        }/* www. jav a2  s . c o  m*/
    };
    File[] files = dir.listFiles(fileFilter);

}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("/data");

    if (file.isDirectory()) {
        String[] files = file.list();

        if (files.length > 0) {
            System.out.println("The " + file.getPath() + " is not empty!");
        }/*from   w  w w  .j  a  va2  s .  c o m*/
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    File file = new File(".");

    if (file.isDirectory()) {
        String[] files = file.list();
        for (int i = 0; i < files.length; i++)
            System.out.println(files[i]);
    } else {//  ww w.  j  av a 2  s .  co  m
        FileReader fr = new FileReader(file);
        BufferedReader in = new BufferedReader(fr);
        String line;
        while ((line = in.readLine()) != null)
            System.out.println(line);
    }
}

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();//from   w w  w.  j  a  va 2  s.  c  o m

        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");
            }
        }
    } else {
        System.out.println(dirname + " is not a directory");
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    boolean bool = f.isDirectory();

    String path = f.getPath();//from  w ww . ja v  a 2 s. c o  m

    System.out.println(path + " is directory? " + bool);

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

    bool = f.isDirectory();

    path = f.getPath();

    System.out.print(path + " is directory? " + bool);

}

From source file:GuaranteeAFile.java

public static void main(String[] args) {

    String filename = "C:/myFile.txt";
    File aFile = new File(filename);
    if (aFile.isDirectory()) {
        System.out.println("The path " + aFile.getPath() + " does not specify a file. Program aborted.");
        System.exit(1);/*from   w w  w .  j  ava  2 s .  com*/
    }
    if (!aFile.isFile()) {
        aFile = aFile.getAbsoluteFile();
        File parentDir = new File(aFile.getParent());
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
    }
    FileOutputStream outputFile = null;
    try {
        outputFile = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void main(String[] args) {
    File directory = new File("c:\\");

    if (!directory.isDirectory()) {
        System.out.println("No directory provided");
        return;//  w  w w  .j  a  va 2s  . co  m
    }

    FilenameFilter filefilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    };

    String[] filenames = directory.list(filefilter);

    for (String name : filenames) {
        System.out.println(name);
    }
}

From source file:DirList.java

public static void main(String[] args) {
    System.out.print("\nEnter a path: ");
    String path = sc.nextLine();/*from   w ww . j a  v  a 2 s  .com*/
    File dir = new File(path);
    if (!dir.exists() || !dir.isDirectory())
        System.out.println("\nThat directory doesn't exist.");
    else {
        System.out.println("\nListing directory tree of:");
        System.out.println(dir.getPath());
        listDirectories(dir, "  ");
    }
}

From source file:Main.java

public static void main(String[] args) {
    // Create a file filter to show only a directory or .doc files
    FileFilter filter = new FileFilter() {
        @Override/*w  ww  .  j a  va  2s.c  om*/
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String fileName = f.getName().toLowerCase();
            if (fileName.endsWith(".doc")) {
                return true;
            }

            return false; // Reject any other files
        }

        @Override
        public String getDescription() {
            return "Word Document";
        }
    };

    // Set the file filter
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(filter);

    int returnValue = fileChooser.showDialog(null, "Attach");
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        // Process the file
    }

}

From source file:Compress.java

public static void main(String args[]) throws IOException {
    String from = ".";
    File f = new File(from);
    boolean directory = f.isDirectory(); // Is it a file or directory?

    Compress.zipDirectory(from, from + ".zip");
    Compress.gzipFile(from, from + ".gz");
}