本文共 1478 字,大约阅读时间需要 4 分钟。
java创建一个新文件
The task is to create a new empty file in Java.
任务是用Java创建一个新的空文件。
Creating a new empty file
创建一个新的空文件
To create a new empty file in java – we use of "File" class. The is called with the File object which should be initialized with the file name (and path if you want to specify).
要在Java中创建一个新的空文件,我们使用“ File”类的 。 用File对象调用 ,该对象应使用文件名(和路径(如果要指定)初始化)。
The returns a Boolean value if file created successfully it returns "true" otherwise it returns "false".
如果文件创建成功,则将返回一个布尔值,它将返回“ true”,否则将返回“ false”。
Package to use: import java.io.*;
要使用的包: import java.io. *;
Syntax:
句法:
//file object creation File file = new File("D://sample.txt"); //file creation file.createNewFile(); Output: true
//Java code to create a new empty fileimport java.io.*;public class Main { public static void main(String[] args) { //creating file object File file = new File("sample.txt"); //following syntax can be used to define //path (D://sample.txt) also //File file = new File("D://sample.txt"); //variable to store result //'true' - file created successfully //'false' - file is not created successfully boolean result = false; try { result = file.createNewFile(); System.out.println(file.getPath() + " created successfully..."); } catch (IOException exp) { System.out.println("Error while creating file: " + exp); } }}
Output
输出量
sample.txt created successfully...
翻译自:
java创建一个新文件
转载地址:http://qgazd.baihongyu.com/