Checking a file

2 0 0
                                    

Files - a file class that contains static methods that operates on files and directories.

Checking a file - you need to verify if the file does exist and check what you can do with it. Take note that before using the program, you need to create a file or a text document first to test or else the result would be your files does exist all the time.

=================

Class PracticingFile{

    public static void main(String[] args) throws IOException{

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your file below:");

        String direction = scanner.nextLine();

        Path objectPath1 = Path.of(direction);

        Path path2 = objectPath1.toAbsolutePath();

        if(Files.exist(path2)){

            System.out.println("Your file: " + path2.getParent());

            if(Files.isReadable(path2)){

                 System.out.println("file: Readable");

            }else {

                System.out.println("file: Not readable");

            }

        }else {

            System.out.println("Your file does not exist.");

       }

       scanner.close();

    }

}

=================

Result:

Enter your file below:

YourTempFile.txt

Your file: c:\Folder1\subfolder\anotherSubfolder\YourTempFile.txt

file: Readable


Note: Other static functions of Files class to use for checking are isWritable(), isHidden(), isSymbolicLink(), isExecutable() and so on.

Java ProgrammingWhere stories live. Discover now