java临时文件下载_Java临时文件

本文介绍了如何使用java.io.File类在Java中创建临时文件。通过指定前缀和后缀,可以确保文件的安全性,防止文件名被猜测。当未指定目录时,文件将存储在操作系统默认的临时目录下。示例程序演示了创建临时文件的过程。
摘要由CSDN通过智能技术生成

java临时文件下载

java.io.File class can be used to create temp file in java. Sometimes we need to create temporary files to be used by our application.

java.io.File类可用于在Java中创建临时文件。 有时我们需要创建临时文件以供我们的应用程序使用。

Java临时文件 (Java Temp File)

There are two methods in File class that we can use to create temp file in java.

File类中有两种方法可用于在Java中创建临时文件。

  1. createTempFile(String prefix, String suffix, File directory): This method creates a temp file with given suffix and prefix in the directory argument.

    The directory should already be existing and should be a directory, else it throws exception.

    The file name is created with random long number, so the file name becomes prefix+random_long_no+suffix.

    This is done to make the application safe as it will be impossible to guess the file name and since application has instance of temp file, we can use it. The prefix String should be minimum three characters long. If suffix is null, “.tmp” suffix is used.

    If directory is null, then temp file is created in operating system temp directory.

    createTempFile(String prefix, String suffix, File directory) :此方法创建一个临时文件,在目录参数中具有给定的后缀和前缀。

    该目录应该已经存在,并且应该是目录,否则会抛出exception

    文件名是使用随机prefix+random_long_no+suffix创建的,因此文件名变为prefix+random_long_no+suffix

    这样做是为了使应用程序安全,因为不可能猜测文件名,并且由于应用程序具有临时文件实例,因此我们可以使用它。 前缀字符串的长度至少应为三个字符。 如果后缀为null,则使用“ .tmp”后缀。

    如果directory为null,则在操作系统的temp目录中创建临时文件。

  2. createTempFile(String prefix, String suffix): It’s easy way to create temp file in operating system temp directory.

    createTempFile(String prefix, String suffix) :这是在操作系统临时目录中创建临时文件的简便方法。

Java Temp文件示例 (Java Temp File Example)

Here is a small java temp file example program.

这是一个小的Java临时文件示例程序。

package com.journaldev.files;

import java.io.File;
import java.io.IOException;

public class JavaTempFile {

	public static void main(String[] args) {
		try {
			File tmpFile = File.createTempFile("data", null);
			File newFile = File.createTempFile("text", ".temp", new File("/Users/pankaj/temp"));
			System.out.println(tmpFile.getCanonicalPath());
			System.out.println(newFile.getCanonicalPath());
			// write,read data to temporary file like any normal file

			// delete when application terminates
			tmpFile.deleteOnExit();
			newFile.deleteOnExit();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Output of the above java temp file program is:

上面的Java临时文件程序的输出是:

/private/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/data225458400489752329.tmp
/Users/pankaj/temp/text2548249124983543974.temp

That’s all about creating temp file in java.

这就是在Java中创建临时文件的全部内容。

翻译自: https://www.journaldev.com/971/java-temp-file

java临时文件下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值