How to Check if an Object is a File Using Java 


               


How to Check if an Object is a File Using Java

Java


In this article, we show how to check if an object is a file using PHP.

This is to see whether an object is a file rather than a directory.

How this works is we create an object of the File class and set it equal to a file name. We can then test this object by invoking the Java isFile() method on the object. If the method returns true, the object is a file. If it does not, it is not a file.

Java has a built-in isFile() that allows us to check specifically to see whether an object is a file or not.

In real-world code, the isFile() more than likely wouldn't check a single File object. Usually, this function is used when you are looping through all files in a directory. You then can use the isFile() function to filter out files from directories.

However, in this article, for demonstration purposes, we show how to find out if a single object is a file. You then can use this code as a base to loop through an entire directory and see if specific files are files or subdirectories in a directory.

The code to check whether an object is a file is shown below.



So in the file above we create a class called Filecheck.

We then put in our main() method.

In the main method, we create a file object, f, and pass the parameter named "books.pdf" into the this File object.

So at this point in the code, the File object f has the value of books.pdf.

We then call the isFile() function through the if statement.

The function is f.isFile(). This function returns a boolean value of either true or false. If the object is a file in the directory, it returns true. If the object is not a file, it returns false. We output the appropriate message based on this boolean value.

Since we didn't specify the path in the parameter passed into the File object, the Java program looks in the current directory, the one storing this Java file, to look for the file name books.pdf.

If you want to specify a directory other than the current directory, then you will have to specify the complete path.

Since this differs according to what operating system you are using, this is the only part of Java that becomes platform dependent.

In a Windows computer, you normally specify the drive you are using with a letter such as the C drive and then specify the path from there to the object that you want to check is a file.

So if you have an object you want to check to see whether it is a file in the C:\Users\ directory, then you would write this into the string as "C:\\Users\\"

This is because "\" is an escape character; it's used for escaping strings. To show "\" in a string, you would have to put double forward slashes "\\".



And this is all that is required to check whether an object of the File class is a file or not.


Related Resources

How to Check if a File Exists Using Java

How to Check if a File is a Directory Using Java

How to Create a New File Using Java

How to Rename a File Using Java

How to Delete a File Using Java

How to Create a New Directory Using Java

How to List All Files in a Directory Using Java

How to Read a File Using Java

How to Write Data to a File Using Java

How to Get the Size of a File Using Java




HTML Comment Box is loading comments...