add method

This commit is contained in:
huzhongying 2022-02-15 10:49:53 +08:00
parent f8acf05f7c
commit 59a43b4371
2 changed files with 53 additions and 0 deletions

View File

@ -891,6 +891,48 @@ public class FileUtil extends PathUtil {
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>
* 创建后的文件名为 prefix[Randon].tmp

View File

@ -452,4 +452,15 @@ public class FileUtilTest {
List<String> list = ListUtil.toList("a", "b", "c");
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"));
}
}