无法创建临时文件

发布于 2024-09-26 06:53:40 字数 527 浏览 7 评论 0原文

我正在使用这段代码创建一个临时文件:

String tmpDirectoryOp = System.getProperty("java.io.tmpdir");
File tmpDirectory = new File(tmpDirectoryOp);
File fstream = File.createTempFile("tmpDirectory",".flv", tmpDirectory);
FileOutputStream fos = new FileOutputStream(fstream);
DataOutputStream dos=new DataOutputStream(fos);

dos.writeChars("Write something");

fstream.deleteOnExit();

fos.close();
dos.close();

但是我的项目文件夹中没有 tmpDirectory.flv 。 write语句是一个循环,需要相当长的时间才能完成,所以问题不在于文件在我看到它之前就被删除了。
有什么想法吗?提前致谢

I am using this piece of code to create a temporary file:

String tmpDirectoryOp = System.getProperty("java.io.tmpdir");
File tmpDirectory = new File(tmpDirectoryOp);
File fstream = File.createTempFile("tmpDirectory",".flv", tmpDirectory);
FileOutputStream fos = new FileOutputStream(fstream);
DataOutputStream dos=new DataOutputStream(fos);

dos.writeChars("Write something");

fstream.deleteOnExit();

fos.close();
dos.close();

But there is no tmpDirectory.flv in my project folder. The write sentence is in a loop, which takes quite long time to finish, so the problem is not that the file is deleted before I could see it.
Any idea? Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妖妓 2024-10-03 06:53:40

默认情况下创建一个空文件
临时文件目录
,使用
给定要生成的前缀和后缀
它的名字。调用这个方法是
相当于调用
createTempFile(前缀,后缀,空)。

获取操作系统的临时目录

System.getProperty("java.io.tmpdir");  

您可以使用您已执行的 deleteOnExit()

public void deleteOnExit()
请求文件或目录
由这个抽象路径名表示
虚拟机删除时
终止。将尝试删除
仅用于正常终止
虚拟机,定义为
Java 语言规范。一次
已请求删除,但未请求
可以取消请求。这
因此,该方法应与
照顾。

注意:不应使用此方法
对于文件锁定,作为结果
协议无法工作
可靠。 FileLock 设施应该
来代替使用。

Creates an empty file in the default
temporary-file directory
, using the
given prefix and suffix to generate
its name. Invoking this method is
equivalent to invoking
createTempFile(prefix, suffix, null).

You can get temp dir for your operating system using

System.getProperty("java.io.tmpdir");  

You have executed deleteOnExit()

public void deleteOnExit()
Requests that the file or directory
denoted by this abstract pathname be
deleted when the virtual machine
terminates. Deletion will be attempted
only for normal termination of the
virtual machine, as defined by the
Java Language Specification. Once
deletion has been requested, it is not
possible to cancel the request. This
method should therefore be used with
care.

Note: this method should not be used
for file-locking, as the resulting
protocol cannot be made to work
reliably. The FileLock facility should
be used instead.

纵山崖 2024-10-03 06:53:40

!!请关闭直播!!

File fstream = File.createTempFile("tmpDirectory",".flv"); 
FileOutputStream fos = new FileOutputStream(fstream); 
DataOutputStream dos=new DataOutputStream(fos); 

dos.writeChars("Write something"); 

fstream.deleteOnExit(); 

**

fos.close();
dos.close();

**

!! Please close the streams !!

File fstream = File.createTempFile("tmpDirectory",".flv"); 
FileOutputStream fos = new FileOutputStream(fstream); 
DataOutputStream dos=new DataOutputStream(fos); 

dos.writeChars("Write something"); 

fstream.deleteOnExit(); 

**

fos.close();
dos.close();

**

多孤肩上扛 2024-10-03 06:53:40

您查看过 /tmp 文件夹吗?

如果要在指定文件夹中创建临时文件,则需要 3 参数 createTempFile 函数

Have you looked in your /tmp folder?

If you want to create a temporary file in a specified folder, you need the 3 param createTempFile function

恬淡成诗 2024-10-03 06:53:40

尝试刷新并关闭流。

Try to flush and close the stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文