導航:首頁 > 編程語言 > javacreatenewfile

javacreatenewfile

發布時間:2023-06-26 16:58:00

Ⅰ 「createNewFile()」方法是做什麼的

方法自動創建此抽象路徑名的新文件。文件鎖設備應該使用這種方法,文件鎖定會導致協議無法進行可靠地工作。
1.聲明
以下是createNewFile()方法的聲明:
public boolean createNewFile()

2.參數
NA
3.返回值
此方法返回true,如果指定的文件不存在,並已成功創建。如果該文件存在,該方法返回false。
4.異常
IOException -- 如果發生I/ O錯誤
SecurityException --如果SecurityManager.checkWrite(java.lang.String) 方法拒絕寫入許可權的文件
5.例子
下面的示例演示createNewFile()方法的用法。
package com.yii;

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();

// print
System.out.println("File created: "+bool);

}catch(Exception e){
e.printStackTrace();
}
}
}

Ⅱ java中createNewFile怎麼使用

java中createNewFile方法主要是如果該文件已經存在,則不創建,返回一個false,如果沒有,則返回true,如下代碼:

packagecom.yii;

importjava.io.File;

publicclassFileDemo{
publicstaticvoidmain(String[]args){

Filef=null;
booleanbool=false;

try{
//createnewfile
f=newFile("test.txt");//在默認路徑創建一個file類

//
bool=f.createNewFile();//返回true或者false判斷該文件是否已經創建好

//prints
System.out.println("Filecreated:"+bool);

//deletesfilefromthesystem
f.delete();

//delete()isinvoked
System.out.println("delete()methodisinvoked");

//
bool=f.createNewFile();

//print
System.out.println("Filecreated:"+bool);

}catch(Exceptione){
e.printStackTrace();
}
}
}
讓我們編譯和運行上面的程序,這將產生以下結果:
Filecreated:false
delete()methodisinvoked
Filecreated:true

Ⅲ 不調用createNewFile()在java中怎麼創建一個新的文件

new java.io.FileOutputStream("newFile.txt");

就可以生成文件 了。。。。。。。。

Ⅳ java中File類的createNewFile()

File file=new File("1.txt");
if(!file.exists())
file.createNewFile();
就創建好了,隨便什麼擴展名都是可以的

Ⅳ java中創建文件

一般都可以通過」new file「的形式來完成文件創建。代碼如下:
import java.io.*;
public class filename {
public static void main(String[] arg) throws IOException { //以下操作可能出現異常,必須放入try塊中
try{
File path=new File("F:\\filepath"); //先設置文件路徑
File dir=new File(path,"filename.txt"); //設置在文件路徑下創建新文件的名稱
if(!dir.exists()) //判斷文件是否已經存在
dir.createNewFile(); //如果不存在的話就創建一個文件
}
catch(Exception e){ //如果存在就會報錯,
System.out.print("創建失敗");//輸出創建失敗信息,也就證明當前要創建的文件已經存在。
}
}
}

閱讀全文

與javacreatenewfile相關的資料

熱點內容
魔鬼訓練程序員 瀏覽:686
php上傳大文件失敗 瀏覽:602
sw伺服器指定埠怎麼填 瀏覽:189
java有哪些數組 瀏覽:984
程序員戴手錶影響工作嗎 瀏覽:235
游戲皇後解壓視頻 瀏覽:367
c語言怎麼打開文件編譯 瀏覽:436
手機上什麼app可以設計logo 瀏覽:800
pid演算法單片機 瀏覽:375
python數據精度 瀏覽:632
管什麼小女孩App 瀏覽:192
phppdf轉換成圖片 瀏覽:468
十八講pdf 瀏覽:619
mysql導入壓縮文件 瀏覽:22
usb控制單片機 瀏覽:906
你為什麼喜歡安卓手機 瀏覽:863
阿里雲伺服器購買和使用 瀏覽:389
單片機sp1 瀏覽:282
大學生玩貓咪解壓 瀏覽:794
php附近的演算法 瀏覽:346