您的位置:首页 > 其它

public boolean createNewFile () 解释

2013-01-19 18:31 495 查看
boolean java.io.File.createNewFile() throws IOException

public boolean createNewFile ()

Since: API Level 1

Creates a new, empty file on the file system according to the path information stored in this file. This method returns true if it creates a file, false if the file already existed. Note that it returns false even if the file is not a file (because it's
a directory, say).

在文件系统中根据保存在文件中的路径信息,创建一个新的空文件。如果创建成功就会返回true,如果文件存在返回false。注意:如果他已经存在但不是文件,可能是目录也会返回false。

This method is not generally useful. For creating temporary files, use createTempFile(String, String) instead. For reading/writing files, use FileInputStream, FileOutputStream, or RandomAccessFile, all of which can create files.

这个方法不是很常用。创建临时文件通常使用createTempFile(String,String)替代。创建读/写文件使用FileInputStream,FileOutputStream,或者RandmomAccessFile,他们都可以创建文件。

Note that this method does not throw IOException if the file already exists, even if it's not a regular file. Callers should always check the return value, and may additionally want to call isFile().

注意,这个方法如果遇到文件存在不会抛出异常,及时是一个不规则的文件。调用者应该总是检查返回值,或者需要额外的调用isFile()来判断。

Returns

true if the file has been created, false if it already exists.

Throws

IOException if it's not possible to create the file.

如果不能创建文件就会抛出异常。

例如:

File file = new File(SDPATH + fileName);

file.createNewFile();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: