小編給大家分享一下createNewFile()方法有什么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
方法自動(dòng)創(chuàng)建此抽象路徑名的新文件。文件鎖設(shè)備應(yīng)該使用這種方法,文件鎖定會(huì)導(dǎo)致協(xié)議無法進(jìn)行可靠地工作。
1.聲明
以下是createNewFile()方法的聲明:
public boolean createNewFile()
2.參數(shù)
NA
3.返回值
此方法返回true,如果指定的文件不存在,并已成功創(chuàng)建。如果該文件存在,該方法返回false。
4.異常
IOException -- 如果發(fā)生I/ O錯(cuò)誤
SecurityException --如果SecurityManager.checkWrite(java.lang.String) 方法拒絕寫入權(quán)限的文件
5.例子
下面的示例演示createNewFile()方法的用法。
package com.yiibai;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try{
// create new file
f = new File("test.txt");
// tries to create new file in the system
bool = f.createNewFile();
// prints
System.out.println("File created: "+bool);
// deletes file from the system
f.delete();
// delete() is invoked
System.out.println("delete() method is invoked");
// tries to create new file in the system
bool = f.createNewFile();
System.out.println("File created: "+bool);
}catch(Exception e){
e.printStackTrace();
}
}
}
以上是createNewFile()方法有什么用的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!