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

熱點內容
phpmysqlpdomysqli 瀏覽:802
php修改sql語句 瀏覽:714
android有道api 瀏覽:381
撓耳營水表用的什麼app 瀏覽:49
戰錘40kpdf 瀏覽:375
java判斷字元串是漢字 瀏覽:462
2017初級教材pdf 瀏覽:451
松下空調壓縮機品牌 瀏覽:817
python復選框 瀏覽:746
反詐中心app是什麼時候出來的 瀏覽:586
魔獸世界需要什麼伺服器地址 瀏覽:189
啥是單片機休眠 瀏覽:104
什麼音樂app最好 瀏覽:927
牙膏自製解壓神器 瀏覽:329
卸載linuxwps 瀏覽:228
threadjavarunnable 瀏覽:360
一元線性回歸計演算法 瀏覽:950
刷寶app是什麼鬼 瀏覽:744
linux動態載入模塊 瀏覽:25
javamd5工具類 瀏覽:76