導航:首頁 > 編程語言 > 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相關的資料

熱點內容
高中畢業當程序員 瀏覽:245
php標簽屬性大全 瀏覽:897
遠程訪問伺服器ip地址 瀏覽:312
程序員吃雞蛋炒菜 瀏覽:173
在哪裡看俄羅斯電視劇app 瀏覽:308
怎麼找資料庫伺服器地址 瀏覽:487
伺服器調試怎麼翻譯 瀏覽:921
php如何處理ajax請求 瀏覽:211
php數組下標存在 瀏覽:707
php獲取ip歸屬地 瀏覽:175
撩女程序員怎麼辦 瀏覽:508
百度伺服器做什麼 瀏覽:193
打開軟體加速伺服器有什麼危害 瀏覽:87
php去除數組下標 瀏覽:794
ipad的app內容哪裡看 瀏覽:284
遇見空間app在哪裡 瀏覽:547
用命令對一個文件內容進行統計 瀏覽:317
華為交換機配置命令縮寫 瀏覽:344
鏈接伺服器出現問題怎麼處理 瀏覽:834
華為手機怎麼打開加密 瀏覽:676