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

How to determine whether it is a file in java

尚
Original
2019-11-20 11:00:233317browse

How to determine whether it is a file in java

In java, you can use java.io.File.isFile() to determine whether it is a file. java.io.File.isFile() checks whether the file representing this abstract path name is a normal file.

The following is the declaration of the java.io.File.isFile() method:

public boolean isFile()

Parameters: NA

Return value

This method returns true if and only if the file representing this abstract pathname is a file, otherwise this method returns false.

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

package com.yiibai;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();
      }
   }}

For more java knowledge, please pay attention to java basic tutorial.

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!

Statement:
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