開心生活站

位置:首頁 > 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