Ⅰ java中careatenewfile方法是什麼意思
File.createNewFilefile類的createnewfile根據抽象路徑創建一個新的空文件,當抽象路徑制定的文件存在時,創建失敗
File.createTempFile 的用途是你想要建立一個檔案暫時使用,但是你不在乎其精確的檔案名,只要不覆蓋到已存在的檔案時。可以制定臨時文件的文件名前綴、後綴及文件所在的目錄,如果不指定目錄,則存放在系統的臨時文件夾下。
Ⅱ java中file類的方法createNewFile()返回值是boolean,為什麼可以直接調用,而不用賦值給一個變數
一般調用createNewFile()這個方法的目的就是創建文件,文件存在與否不是很重要。查看api可以知道當不存在指定文件是會自動創建文件並返回true,當存在指定文件時不會創建該文件並返回false。
還是一句話,用了createNewFile()就比較保險,無論它返回true還是false指定的文件都是會存在地。
Ⅲ 不調用createNewFile()在java中怎麼創建一個新的文件
new java.io.FileOutputStream("newFile.txt");
就可以生成文件 了。。。。。。。。
Ⅳ 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();
System.out.println("Filecreated:"+bool);
}catch(Exceptione){
e.printStackTrace();
}
}
}
讓我們編譯和運行上面的程序,這將產生以下結果:
Filecreated:false
delete()methodisinvoked
Filecreated:true
Ⅳ JAVA里Flie類的creatnewfile與creattempfile有什麼不同
後者的文件建立在默認的臨時文件目錄中 不在當前目錄
createNewFile
public boolean createNewFile()
throws IOException當且僅當不存在具有此抽象路徑名指定名稱的文件時,不可分地創建一個新的空文件。檢查文件是否存在,若不存在則創建該文件,這是單個操作,對於其他所有可能影響該文件的文件系統活動來說,該操作是不可分的。
createTempFile
public static File createTempFile(String prefix,
String suffix)
throws IOException在默認臨時文件目錄中創建一個空文件,使用給定前綴和後綴生成其名稱。調用此方法等同於調用 createTempFile(prefix, suffix, null)。
參數:
prefix - 用於生成文件名的前綴字元串;必須至少是三字元長
suffix - 用於生成文件名的後綴字元串;可以為 null,在這種情況下,將使用後綴 ".tmp"
返回:
表示新建空文件的抽象路徑名
Ⅵ android 下載文件的時候 createNewFile 怎麼回事java.io.IOException: No such file or directory
你創建文件的目錄不存在啊。首先創建那個目錄在創建文件啊。或者直接用FileOutPutStream啊。。
日誌已經說明了沒有這個目錄。如果還搞不定hi我。
Ⅶ JAVA createNewFile()方法疑問
異常
file.createNewFile();
這句話會有異常拋出 因為你雖然知道word.txt是不存在的
但是程序不會提前知道的 所以就會拋出一個異常
Ⅷ java中File類的createNewFile()
File file=new File("1.txt");
if(!file.exists())
file.createNewFile();
就創建好了,隨便什麼擴展名都是可以的
Ⅸ 「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方法是做什麼的什麼時候用,謝謝大神
方法自動創建此抽象路徑名的新文件。文件鎖設備應該使用這種方法,文件鎖定會導致協議無法進行可靠地工作。1.聲明以下是createNewFile()方法的聲明:public boolean createNewFile()2.參數NA3.返回值此方法返回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 filef = new File("test.txt");// tries to create new file in the systembool = f.createNewFile();// printsSystem.out.println("File created: "+bool);// deletes file from the systemf.delete();// delete() is invokedSystem.out.println("delete() method is invoked");// tries to create new file in the systembool = f.createNewFile();// printSystem.out.println("File created: "+bool);}catch(Exception e){e.printStackTrace();}}}