java中可以使用java.io.File.isFile()判讀是否是文件,java.io.File.isFile() 檢查表示此抽象路徑名的文件是否是一個正常的文件。
以下是java.io.File.isFile()方法的聲明:
public boolean isFile()
參數(shù):NA
返回值
當(dāng)且僅當(dāng)表示此抽象路徑名的文件是一個文件該方法返回true,否則該方法返回false。
下面的示例演示java.io.File.isFile()方法的用法
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(); } }}