Example usage for java.io File exists

List of usage examples for java.io File exists

Introduction

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

Prototype

public boolean exists() 

Source Link

Document

Tests whether the file or directory denoted by this abstract pathname exists.

Usage

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C:/Demo.txt");
    System.out.println(file.exists());
}

From source file:FileAttributesDemo.java

public static void main(String[] args) throws IOException {
    // Create a new file, by default canWrite=true, readonly=false
    File file = new File("test.txt");
    if (file.exists()) {
        file.delete();//from  www .j  a va2s  .c o  m
    }
    file.createNewFile();
    System.out.println("Before. canWrite?" + file.canWrite());

    // set to read-only, atau canWrite = false */
    file.setWritable(false);
    System.out.println("After. canWrite?" + file.canWrite());
}

From source file:DirectoryTree.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;/*  ww  w.  ja  v  a  2 s  .  c o m*/
    }
    tree(args[0]);
}

From source file:DirList.java

public static void main(String[] args) {
    System.out.print("\nEnter a path: ");
    String path = sc.nextLine();//  ww w .  j  a  v a 2 s .  c o  m
    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) {
    File file = new File(args[0]);
    if (!file.exists()) {
        System.out.println(args[0] + " does not exist.");
        return;//from  ww  w. ja v a  2 s .c  om
    }
    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]);
        }
    }
}

From source file:Main.java

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

    boolean bool = f.exists();

    if (bool) {//from   ww w  .  j  a  v  a  2 s .c  o m
        long len = f.length();
        String path = f.getPath();
        System.out.print(path + " file length: " + len);
    }

}

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  .  j av  a  2 s  . c o m*/
    }
    if (!(file.isFile() && file.canRead())) {
        System.out.println(file.getName() + " cannot be read from.");
        return;
    }
    try {
        FileInputStream fis = new FileInputStream(file);
        char current;
        while (fis.available() > 0) {
            current = (char) fis.read();
            System.out.print(current);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C://FileIO/ExistsDemo.txt");

    System.out.println(file.exists());
}

From source file:Main.java

public static void main(String[] args) {
    File inputFile = new File("test1.txt");
    if (!inputFile.exists()) {
        System.out.println("The input file " + inputFile.getAbsolutePath() + "  does  not  exist.");
        System.out.println("Aborted the   file reading process.");
        return;//  w  w  w .j a v a  2 s.co m
    }
    try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (fileChannel.read(buffer) > 0) {
            buffer.flip();
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                System.out.print((char) b);
            }
            buffer.clear();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    boolean bool = f.exists();

    // if path exists
    if (bool) {//from   www. ja v a2s.c  o  m
        // returns the time file was last modified
        long millisec = f.lastModified();

        // date and time
        Date dt = new Date(millisec);

        // path
        String path = f.getPath();

        System.out.print(path + " last modified at: " + dt);
    }

}