Home > Java > Javagetting Started > How to determine whether it is a file in java

How to determine whether it is a file in java

(*-*)浩
Release: 2019-11-14 13:23:43
Original
2483 people have browsed it

java.io.File.isFile() Checks whether the file representing this abstract pathname is a normal file.

How to determine whether it is a file in java

Indicates that the file of this abstract path name is a file, this method returns true, otherwise this method returns false.

Exception                                                                                                                                                                                                                         to .lang.String) method denies read access to the file

Example

The following example demonstrates the usage of the java.io.File.isFile() method .

package com.test;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      
      File f = null;
      String path;
      boolean bool = false;
      
      try{
         // create new file
         f = new File("c:");
            
         // true if the file path is a file, else false
         bool = f.isFile();
         
         // get the path
         path = f.getPath();
         
         // prints
         System.out.println(path+" is file? "+ bool);
               
         // create new file
         f = new File("c:/test.txt");
         
         // true if the file path is a file, else false
         bool = f.isFile();
         
         // get the path
         path = f.getPath();
         
         // prints
         System.out.print(path+" is file? "+bool);
         
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}
Copy after login

Let us compile and run the above program, which will produce the following results:

c: is file? false
c:est.txt is file? true
Copy after login

The above is the detailed content of How to determine whether it is a file in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template