public boolean createNewFile () 解释

本文详细介绍了Java中File类的createNewFile()方法。该方法用于根据File对象的路径信息在文件系统中创建新的空文件。文章解释了方法的行为、返回值的意义,并提供了与其他文件创建方法的对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();

以下代码存在读取文件乱码的情况请给与优化 public static void main(String[] args) { String sourceDirectory = "D:\Java\pas\trunk-5.X+\D05源代码\后台\达梦"; String destinationFile = "D:\Java\project\demo\src\main\resources\dm-init.sql"; try { // 创建输出文件,如果文件不存在则自动创建 File file = new File(destinationFile); if (!file.exists()) { file.createNewFile(); } // 打开输出流 FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); // 递归读取源目录中的文件,并将其写入输出文件 readAndWriteFiles(sourceDirectory, bw); // 关闭输出流 bw.close(); System.out.println("文件已写入 " + destinationFile); } catch (IOException e) { e.printStackTrace(); } } private static void readAndWriteFiles(String sourceDirectory, BufferedWriter writer) throws IOException { // 创建目录文件对象 File directory = new File(sourceDirectory); // 检查目录是否存在并且是一个目录 if (!directory.exists() || !directory.isDirectory()) { throw new FileNotFoundException("目录不存在: " + sourceDirectory); } // 列出该目录下的所有文件和子目录,包括隐藏文件 File[] files = directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isFile() && (pathname.getName().endsWith(".sql") || pathname.getName().endsWith(".txt")); } }); // 遍历文件列表 for (File file : files) { // 读取文件内容并写入输出文件 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK")); String line = null; while ((line = br.readLine()) != null) { writer.write(line); writer.newLine(); } br.close(); } // 遍历目录中的子目录并递归读取 File[] subDirectories = directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !pathname.isHidden(); } }); for (File subDirectory : subDirectories) { readAndWriteFiles(subDirectory.getAbsolutePath(), writer); } }
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值