IO-Utils Library

TempFile, CustomMultipartFile, etc.

Лицензия

Лицензия

Категории

Категории

Ant Компиляция и сборка
Группа

Группа

com.antkorwin
Идентификатор

Идентификатор

ioutils
Последняя версия

Последняя версия

0.8
Дата

Дата

Тип

Тип

jar
Описание

Описание

IO-Utils Library
TempFile, CustomMultipartFile, etc.
Ссылка на сайт

Ссылка на сайт

https://github.com/antkorwin/ioutils
Система контроля версий

Система контроля версий

http://github.com/antkorwin/ioutils

Скачать ioutils

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.antkorwin/ioutils/ -->
<dependency>
    <groupId>com.antkorwin</groupId>
    <artifactId>ioutils</artifactId>
    <version>0.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.antkorwin/ioutils/
implementation 'com.antkorwin:ioutils:0.8'
// https://jarcasting.com/artifacts/com.antkorwin/ioutils/
implementation ("com.antkorwin:ioutils:0.8")
'com.antkorwin:ioutils:jar:0.8'
<dependency org="com.antkorwin" name="ioutils" rev="0.8">
  <artifact name="ioutils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.antkorwin', module='ioutils', version='0.8')
)
libraryDependencies += "com.antkorwin" % "ioutils" % "0.8"
[com.antkorwin/ioutils "0.8"]

Зависимости

compile (5)

Идентификатор библиотеки Тип Версия
com.antkorwin : throwable-functions jar 1.2
commons-io : commons-io jar 2.4
com.antkorwin : better-strings jar 0.3
commons-fileupload : commons-fileupload jar 1.4
org.projectlombok : lombok jar 1.18.12

provided (2)

Идентификатор библиотеки Тип Версия
org.springframework : spring-web jar 5.2.5.RELEASE
org.apache.tomcat.embed : tomcat-embed-core jar 9.0.36

test (12)

Идентификатор библиотеки Тип Версия
org.assertj : assertj-core jar 3.9.1
org.mockito : mockito-core jar 2.15.0
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.springframework.boot : spring-boot-starter-test jar 2.2.6.RELEASE
org.springframework.boot : spring-boot-starter-web jar 2.2.6.RELEASE
org.springframework.boot : spring-boot-starter-webflux jar 2.2.6.RELEASE
org.springframework.cloud : spring-cloud-starter-openfeign jar 2.2.3.RELEASE
com.antkorwin : common-utils jar 1.0
com.jupiter-tools : stress-test jar 0.1
org.junit.jupiter : junit-jupiter-api jar 5.6.2
org.junit.jupiter : junit-jupiter-engine jar 5.6.2

Модули Проекта

Данный проект не имеет модулей.

IOUtils

1. Dependencies

<dependency>
    <groupId>com.antkorwin</groupId>
    <artifactId>ioutils</artifactId>
    <version>0.8</version>
</dependency>

2. Automatically deleted temp files in Java

There is a standard API to delete a temporary file in java:

File temp = File.createTempFile("filename", ".tmp");
temp.deleteOnExit();

But this file will be removed only after exit JVM, if your application actively working with temporary files then it can lead to the situation when a free space in the temp folder is over, or sometimes you should restart your application just to clean the temp folder.

To resolve this problem you can use the TempFile from ioutils:

File tmp = TempFile.createEmpty();

this file will be deleted after a GC collecting the tmp object.

Warning
Please use the temp file only by this file reference (tmp variable in the example above) if you create a new reference to the temporary file using an absolute path then you can’t read this file after GC collects the original reference.

Also, we can create non-empty files, creating a file from String:

File tmp = TempFile.createFromString("text file context");

an InputStream variant:

ByteArrayInputStream inputStream = new ByteArrayInputStream("file content".getBytes());
File file = TempFile.createFromInputStream(() -> inputStream);

And you can set an extension to files (.tmp by default):

File file = TempFile.createFromString("data file content", "txt");

3. Read a file from resources in Java

To simplify working with files in the resource folder, you can use the ResourceFile class. You can read about the main methods in this class below, or you can see in the unit test here: ResourceFileTest.java where describing all API of this class.

Read a file as String:

String content = new ResourceFile("filename_in_resource_folder.txt").readAsString();

read as a byte array:

byte[] content = new ResourceFile("/test.txt").readAsByteArray();

get a file from resource:

File file = new ResourceFile("folder/nestedfile").getFile();

read an InputStream from a file in resources:

try (InputStream inputStream = new ResourceFile("test.txt").getInputStream()) {
    ...
}

write a content of file from resources to the OutputStream:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
new ResourceFile("test.txt").write(() -> outputStream);

Версии библиотеки

Версия
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1