!541 新增cn.hutool.core.io.FileUtil#createTempFile重载方法

Merge pull request !541 from huzhongying3/v5-dev
This commit is contained in:
Looly 2022-02-15 03:20:09 +00:00 committed by Gitee
commit 9dcf13ab97
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 56 additions and 0 deletions

View File

@ -891,6 +891,48 @@ public class FileUtil extends PathUtil {
return createTempFile("hutool", null, dir, true); return createTempFile("hutool", null, dir, true);
} }
/**
* 在默认临时文件目录下创建临时文件创建后的文件名为 prefix[Randon].tmp
* 默认临时文件目录由系统属性 <code>java.io.tmpdir<code> 指定
* UNIX 系统上此属性的默认值通常是 <code>"tmp"<code> <code>"vartmp"<code>
* Microsoft Windows 系统上它通常是 <code>"C:\\WINNT\\TEMP"<code>
* 调用 Java 虚拟机时可以为该系统属性赋予不同的值但不保证对该属性的编程更改对该方法使用的临时目录有任何影响
* @return 临时文件
* @throws IORuntimeException IO异常
*/
public static File createTempFile() throws IORuntimeException {
return createTempFile("hutool", null, null, true);
}
/**
* 在默认临时文件目录下创建临时文件创建后的文件名为 prefix[Randon].suffix
* 默认临时文件目录由系统属性 <code>java.io.tmpdir<code> 指定
* UNIX 系统上此属性的默认值通常是 <code>"tmp"<code> <code>"vartmp"<code>
* Microsoft Windows 系统上它通常是 <code>"C:\\WINNT\\TEMP"<code>
* 调用 Java 虚拟机时可以为该系统属性赋予不同的值但不保证对该属性的编程更改对该方法使用的临时目录有任何影响
* @param suffix 后缀如果null则使用默认.tmp
* @param isReCreat 是否重新创建文件删掉原来的创建新的
* @return 临时文件
* @throws IORuntimeException IO异常
*/
public static File createTempFile(String suffix, boolean isReCreat) throws IORuntimeException {
return createTempFile("hutool", suffix, null, isReCreat);
}
/**
* 在默认临时文件目录下创建临时文件创建后的文件名为 prefix[Randon].suffix
* 默认临时文件目录由系统属性 <code>java.io.tmpdir<code> 指定
* UNIX 系统上此属性的默认值通常是 <code>"tmp"<code> <code>"vartmp"<code>
* Microsoft Windows 系统上它通常是 <code>"C:\\WINNT\\TEMP"<code>
* 调用 Java 虚拟机时可以为该系统属性赋予不同的值但不保证对该属性的编程更改对该方法使用的临时目录有任何影响
*
* @param prefix 前缀至少3个字符
* @param suffix 后缀如果null则使用默认.tmp
* @param isReCreat 是否重新创建文件删掉原来的创建新的
* @return 临时文件
* @throws IORuntimeException IO异常
*/
public static File createTempFile(String prefix, String suffix, boolean isReCreat) throws IORuntimeException {
return createTempFile(prefix, suffix, null, isReCreat);
}
/** /**
* 创建临时文件<br> * 创建临时文件<br>
* 创建后的文件名为 prefix[Randon].tmp * 创建后的文件名为 prefix[Randon].tmp

View File

@ -452,4 +452,18 @@ public class FileUtilTest {
List<String> list = ListUtil.toList("a", "b", "c"); List<String> list = ListUtil.toList("a", "b", "c");
FileUtil.appendLines(list, FileUtil.file("d:/test/appendLines.txt"), CharsetUtil.CHARSET_UTF_8); FileUtil.appendLines(list, FileUtil.file("d:/test/appendLines.txt"), CharsetUtil.CHARSET_UTF_8);
} }
@Test
@Ignore
public void createTempFileTest(){
File nullDirTempFile = FileUtil.createTempFile();
Assert.assertTrue(nullDirTempFile.exists());
File suffixDirTempFile = FileUtil.createTempFile(".xlsx",true);
Assert.assertTrue(FileUtil.getSuffix(suffixDirTempFile).equals("xlsx"));
File prefixDirTempFile = FileUtil.createTempFile("prefix",".xlsx",true);
Assert.assertTrue(FileUtil.getPrefix(prefixDirTempFile).startsWith("prefix"));
}
} }