开心生活站

位置:首页 > IT科技 > 

java,file。exists()

IT科技1.33W

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

很多朋友都想知道java file.exists()的用法有哪些?下面就一起来了解一下吧~

java.io.File.exists() 用来测试抽象路径名定义的文件或目录是否存在。

1 语法

public boolean exists()

2 返回值

当且仅当由抽象路径名确定文件是否存在,则该方法返回布尔值true;否则为false。

java file.exists()

3 示例

package com.yiidian;/*** 一点教程网: http://www.yiidian.com*//*** java.io.File.exists()方法的例子*/import java.io.File;public class Demo {undefinedpublic static void main(String[] args) {undefinedFile f = null;boolean bool = false;try {undefined// create new filesf = new File("test.txt");// create new file in the systemf.createNewFile();// tests if file existsbool = f.exists();// printsSystem.out.println("File exists: "+bool);if(bool == true) {undefined// delete() invokedf.delete();System.out.println("delete() invoked");}// tests if file existsbool = f.exists();// printsSystem.out.print("File exists: "+bool);} catch(Exception e) {undefined// if any error occurse.printStackTrace();}}}输出结果为:File exists: truedelete() invokedFile exists: false

标签:fileexists java