Creating and Manipulating Files and Directories创建和处理文件和目录

Creating and Manipulating Files and Directories


File also declares several methods for creating new files and directories and manipulating existing

files and directories. Table 11-4 describes these methods.

Table 11-4. File Methods for Creating New and Manipulating Existing Files and Directories


Method Description
boolean createNewFile() Atomically creates a new, empty file named by this File object’s abstract
pathname if and only if a file with this name doesn’t yet exist. The check
for file existence (and the creation of the file when it doesn’t exist) is a
single operation that’s atomic with respect to all other filesystem activities
that might affect the file. This method returns true when the named file
doesn’t exist and was successfully created, and returns false when the
named file already exists. It throws IOException when an I/O error occurs.


static File createTempFile(String prefix,
String suffix)
Creates an empty file in the default temporary file directory using the
given prefix and suffix to generate its name. This overloaded class
method calls its three-parameter variant,
passing prefix, suffix, and
null to this other method, and returning the other method’s return value.

这个重载类方法调用它的三参数变体。


Suppose you’re designing a text-editor application that a user will use to open a text file and make
changes to its content. Until the user explicitly saves these changes to the file, you want the text file
to remain unchanged.
Because the user doesn’t want to lose these changes when the application crashes or the computer
loses power, you design the application to save these changes to a temporary file every few minutes.
This way, the user has a backup of the changes.
You can use the overloaded createTempFile() methods to create the temporary file. If you don’t
specify a directory in which to store this file, it’s created in the directory identified by the
java.io.tmpdir system property.
You probably want to remove the temporary file after the user tells the application to save or discard
the changes. The deleteOnExit() method lets you register a temporary file for deletion; it’s deleted
when the virtual machine ends without a crash/power loss.
Listing 11-6 presents a TempFileDemo application for experimenting with the createTempFile() and
deleteOnExit() methods.
Listing 11-6. Experimenting with Temporary Files
import java.io.File;
import java.io.IOException;
public class TempFileDemo
{
public static void main(String[] args) throws IOException
{
System.out.println(System.getProperty("java.io.tmpdir"));
File temp = File.createTempFile("text", ".txt");
System.out.println(temp);
temp.deleteOnExit();
}
}
After outputting the location where temporary files are stored, TempFileDemo creates a temporary
file whose name begins with text and ends with the .txt extension. TempFileDemo next outputs the
temporary file’s name and registers the temporary file for deletion upon the successful termination of
the application.
I observed the following output during one run of TempFileDemo (and the file disappeared on exit):
C:\Users\Owner\AppData\Local\Temp\
C:\Users\Owner\AppData\Local\Temp\text3173127870811188221.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值