java.io.file.isfile() 检查表示此抽象路径名的文件是否是一个正常的文件。

表示此抽象路径名的文件是一个文件,该方法返回true,否则该方法返回false。
异常 (推荐学习:java课程)
SecurityException -- 如果安全管理器存在并且其SecurityManager.checkRead(java.lang.String) 方法拒绝对文件的读访问
立即学习“Java免费学习笔记(深入)”;
例子
下面的示例演示java.io.File.isFile()方法的用法。
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();
}
}
}让我们编译和运行上面的程序,这将产生以下结果:
c: is file? false c:est.txt is file? true
以上就是java如何判断是否为文件的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号