Java File類boolean createNewFile()方法(帶示例)

文件類布爾型createNewFile() (File Class boolean createNewFile())

  • This method is available in package java.io.File.createNewFile().

    軟件包java.io.File.createNewFile()中提供了此方法。

  • This method is used to create a new file by using createNewFile() method and this method is accessible with the File object.

    此方法用于通過使用createNewFile()方法創建新文件,并且該方法可通過File對象訪問。

  • If a file is already exists i.e. the name of created file is already exists that means we are not allowed to create a file of same name.

    如果文件已經存在,即創建的文件名已經存在,則意味著我們不允許創建同名文件。

  • The return type of this method is Boolean i.e. it returns true or false if true that means file is successfully created and returns false that means the file already exists.

    此方法的返回類型為Boolean,即返回true或false,如果為true表示文件已成功創建,則返回false表示文件已存在。

Syntax:

句法:

    boolean createNewFile(){
}

Parameter(s):

參數:

We don't pass any object as a parameter in the method of the File.

我們不會在File方法中將任何對象作為參數傳遞。

Return value:

返回值:

The return type of this method is Boolean i.e. it returns true or false if true that means file is successfully created else returns false that means the file already exists.

此方法的返回類型為Boolean,即返回true或false,如果為true則表示文件已成功創建,否則返回false表示文件已存在。

Java程序,演示createNewFile()方法的示例 (Java program to demonstrate example of createNewFile() method)

// import the File class because we will use File class methods
import java.io.File;
// import the Exception class because it may raise an exception 
// when working with files
import java.lang.Exception;
public class CreateFile {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes 
// to escape '\' character sequence for windows otherwise 
// it will be considerable as url.
File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\javafiles.txt");
File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
// By using createNewFile()create a new file named 
// javafiles.txt because file is not exists 
// (i.e it returns true) so this method creates empty file.
if (file1.createNewFile())
System.out.println("File created Successfully" + " " + file1.getName());
else
System.out.println("File already exists " + file1.getName());
// By using createNewFile() is not create a new file 
// named myjava.txt because the name of this file is 
// already exists(i.e. it returns false) so we will 
// get a message File already exists .
if (file2.createNewFile())
System.out.println("File created Successfully" + file2.getName());
else
System.out.println("File already exists" + " " + file2.getName());
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

Output

輸出量

D:\Programs>javac CreateFile.java
D:\Programs>java CreateFile
File already exists C:\Users\computer clinic\OneDrive\Articles\javafiles.txt
File already exists C:\Users\computer clinic\OneDrive\Articles\myjava.txt

翻譯自: https://www.includehelp.com/java/file-class-boolean-createnewfile-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542396.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542396.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542396.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle ? SQL執行過程

1.sql執行過程1>解析(判斷對象是否存在,是否有權限查詢,語義解析,檢查緩存中是否有相同的SQL等等)2>優化(CBO確定優化模式,確定訪問路徑,聯接順序,過程中通過很多綜…

vue-video-player修改src就會報錯_4、修改入口點代碼

在riscv上電時,會進行CPU自檢,然后跳轉到bootloader處執行。bootloader設置好kernel的運行環境后,從硬盤加載kernel到內存,最后再跳轉到kernel入口地址。我們采用的bootloader為OpenSBI,被加載到0x80000000地址&#x…

數碼管超聲波c語言黑51,51單片機開發板-超聲波測距-數碼管顯示

《51單片機開發板-超聲波測距-數碼管顯示》由會員分享,可在線閱讀,更多相關《51單片機開發板-超聲波測距-數碼管顯示(16頁珍藏版)》請在人人文庫網上搜索。1、計算機技術系項目工作報告課程名稱單片機開發板設計與制作實訓班級學號姓名項目名稱超聲波測距…

java 方法 示例_Java ArrayDeque帶有示例的removeFirstOccurrence()方法

java 方法 示例ArrayDeque類removeFirstOccurrence()方法 (ArrayDeque Class removeFirstOccurrence() method) removeFirstOccurrence() method is available in java.lang package. removeFirstOccurrence()方法在java.lang包中可用。 removeFirstOccurrence() method is use…

社交應用動態九宮格圖片的規則

這里主要以微信和QQ空間為作為研究對象,得到的結論如下。 QQ空間里的動態 iOS設備,以iPhone6為分界 iPhone6及以上分辨率的設備: 當寬且高同時 > 512px時,判斷 寬/高的比例值:大于 2時,以高度為基準&…

c語言實現鏈表結構6,用c語言實現的鏈表結構--數據結構實驗

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include"stdio.h"//使用new指針來將臨時變量重新初始化#include"stdio.h"typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;void InitList(LinkList &L)//…

splunk中 如何隱藏input_翻糖制作中,如何避免裂縫,如何隱藏裂縫,如何防粘?...

翻糖蛋糕 因精致的樣子和栩栩如生的各種造型深得人們的喜愛,它不僅滿足了人們對蛋糕口味及裝飾日益多樣化的需求,同時也在動手制作的過程中,享受到美食與生活的無窮樂趣。不過裂縫,不平整,干燥對翻糖作品來說無疑是噩夢…

Java DataInputStream readUnsignedByte()方法(帶示例)

DataInputStream類readUnsignedByte()方法 (DataInputStream Class readUnsignedByte() method) readUnsignedByte() method is available in java.io package. readUnsignedByte()方法在java.io包中可用。 readUnsignedByte() method is used to read 1 byte (i.e. 8 bit) of …

wpf中groupbox有什么用_展示設計中的標攤是什么 用的什么材料

經常聽從事展示設計的工作人員說起標攤,那什么是標攤呢?顧名思義,標攤就是通用標準的國際展會攤位的縮寫。但是不少人看到干巴巴的詞語還是不能理解。那么這篇文章從用途、材料等方面來詳細介紹標攤究竟是什么。 標攤的主要材質是什么一般來說…

Java BigInteger類| nextProbablePrime()方法與示例

BigInteger類nextProbablePrime()方法 (BigInteger Class nextProbablePrime() method) nextProbablePrime() method is available in java.math package. nextProbablePrime()方法在java.math包中可用。 nextProbablePrime() method is used to get the next probable prime n…

SQL 行轉列的兩種做法

if object_id(tb)is not null drop table tbGocreate table tb(姓名 varchar(10),課程 varchar(10),分數 int)insert into tb values(張三,語文,74)insert into tb values(張三,數學,83)insert into tb values(張三,物理,93)insert into tb values(李四,語文,74)insert into tb…

android一個工程的xml怎么引用另外一個工程的xml,如何在Android中使用XML引用庫中另一個包的字符串?...

Androiddocumentation告訴我,我可以使用“包名”訪問另一個包中的字符串,無論這意味著什么:[:]/所以在我的清單中,我想訪問一個字符串,我已將其放在一個單獨的庫項目中,在com.globalmentor.android包中 – 畢竟我的R類就是這樣:android:label"com.g…

kotlin 類和對象_Kotlin程序| 類和對象的示例(帶有學生數據)

kotlin 類和對象In the below program, we are creating a student class to input and print the student data like name, age. It is a simple example of creating class in Kotlin. 在下面的程序中,我們將創建一個學生班級,以輸入和打印學生數據&am…

python 復數數組_python的數組運算及推導式的運用

運行方式:縮進 TAb(四格)或者space(1格)注釋:#可以在程序后注釋文字 或者 ... ,""" ... """ 可以多行注釋 中間全為注釋數值輸出 print(...)圖形輸出 import matplotlib as pltplt.show()列表 list[] #字符串…

ActiveX: 如何用.inf和.ocx文件生成cab文件

ActiveX: 如何用.inf和.ocx文件生成cab文件 轉載于:https://www.cnblogs.com/time-is-life/p/5977962.html

Android工具里沒有Android,android – AppCompat工具欄沒有顯示

在主題中聲明.NoActionBar之后,以及將工具欄放在布局中,我的工具欄不會顯示.我最終得到的正是你在宣布沒有動作欄時所期望的 – 沒有動作欄.這是布局:activity_home.xml:layout"layout/app_bar_home"android:layout_width"match_parent&q…

操作系統中的處理機調度調度_操作系統中的多處理器調度

操作系統中的處理機調度調度多處理器操作系統 (Multiprocessor Operating system) A multiprocessor system consists of several processors which share memory. In the multiprocessor, there is more than one processor in the system. The reason we use multiprocessor …

轉:Delphi2010新發現-類的構造和析構函數功能

Delphi2010發布了. 雖然憑著對Delphi的熱愛第一時間就安裝了,但是現在可能是年紀大了,對新事物缺乏興趣了.一直都沒有仔細研究. 今天有點時間試了一下新功能. 本來C#和Delphi.NET是支持類的構造函數/析構函數的(注意不是實例的構造和析構).也就是在模塊初始化/卸載的時候會調用…

sed 替換_sed命令批量替換文件內容

“ 開發人員有時會大批量替換文件內容,sed命令是一個很好用的工具。”01—暴力替換方式近期有個臨時任務,將系統中所有"帳"替換為"賬",那"帳"和"賬"有啥區別呢;1、賬的部首是貝;帳的部首是巾。2、賬是關于貨幣、…

android 模仿uc標簽頁,模仿UCweb菜單 - 白羽雕弓 - 博客園

UCWeb的菜單看起來不錯,自己想模仿做一個,苦惱一直沒有思路google了幾天,終于找到一個帖子 http://www.eoeandroid.com/viewthread.php?tid28824按照上面提供的思路實現了1、保留menu按鍵作用2、用popupwindow作為菜單顯示容器3、用GridVie…