日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java File类boolean createNewFile()方法(带示例)

發布時間:2025/3/11 java 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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());elseSystem.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());elseSystem.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

總結

以上是生活随笔為你收集整理的Java File类boolean createNewFile()方法(带示例)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。